aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+admin/system/logs/log-row.model.ts
blob: e83c7b064f71c4952cff60a19e496c76c6b807b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import omit from 'lodash-es/omit'
import { logger } from '@root-helpers/logger'
import { ServerLogLevel } from '@shared/models'

export class LogRow {
  date: Date
  localeDate: string
  level: ServerLogLevel
  message: string
  meta: string

  by: string
  domain: string
  action: string

  constructor (row: any) {
    this.date = new Date(row.timestamp)
    this.localeDate = this.date.toLocaleString()
    this.level = row.level
    this.message = row.message

    const metaObj = omit(row, 'timestamp', 'level', 'message', 'label')

    if (Object.keys(metaObj).length !== 0) this.meta = JSON.stringify(metaObj, undefined, 2)

    if (row.level === 'audit') {
      try {
        const message = JSON.parse(row.message)

        this.by = message.user
        this.domain = message.domain
        this.action = message.action

        this.meta = JSON.stringify(message, null, 2)
        this.message = ''
      } catch (err) {
        logger.error('Cannot parse audit message.', err)
      }
    }
  }
}