1 | 'use strict'
|
2 |
|
3 | const chalk = require('chalk')
|
4 | const jmespath = require('jmespath')
|
5 | const colors = require('./lib/colors')
|
6 | const { ERROR_LIKE_KEYS, MESSAGE_KEY, TIMESTAMP_KEY } = require('./lib/constants')
|
7 | const {
|
8 | isObject,
|
9 | prettifyErrorLog,
|
10 | prettifyLevel,
|
11 | prettifyMessage,
|
12 | prettifyMetadata,
|
13 | prettifyObject,
|
14 | prettifyTime
|
15 | } = require('./lib/utils')
|
16 |
|
17 | const bourne = require('@hapi/bourne')
|
18 | const jsonParser = input => {
|
19 | try {
|
20 | return { value: bourne.parse(input, { protoAction: 'remove' }) }
|
21 | } catch (err) {
|
22 | return { err }
|
23 | }
|
24 | }
|
25 |
|
26 | const defaultOptions = {
|
27 | colorize: chalk.supportsColor,
|
28 | crlf: false,
|
29 | errorLikeObjectKeys: ERROR_LIKE_KEYS,
|
30 | errorProps: '',
|
31 | levelFirst: false,
|
32 | messageKey: MESSAGE_KEY,
|
33 | messageFormat: false,
|
34 | timestampKey: TIMESTAMP_KEY,
|
35 | translateTime: false,
|
36 | useMetadata: false,
|
37 | outputStream: process.stdout,
|
38 | customPrettifiers: {},
|
39 | hideObject: false
|
40 | }
|
41 |
|
42 | module.exports = function prettyFactory (options) {
|
43 | const opts = Object.assign({}, defaultOptions, options)
|
44 | const EOL = opts.crlf ? '\r\n' : '\n'
|
45 | const IDENT = ' '
|
46 | const messageKey = opts.messageKey
|
47 | const levelKey = opts.levelKey
|
48 | const levelLabel = opts.levelLabel
|
49 | const messageFormat = opts.messageFormat
|
50 | const timestampKey = opts.timestampKey
|
51 | const errorLikeObjectKeys = opts.errorLikeObjectKeys
|
52 | const errorProps = opts.errorProps.split(',')
|
53 | const customPrettifiers = opts.customPrettifiers
|
54 | const ignoreKeys = opts.ignore ? new Set(opts.ignore.split(',')) : undefined
|
55 | const hideObject = opts.hideObject
|
56 |
|
57 | const colorizer = colors(opts.colorize)
|
58 | const search = opts.search
|
59 |
|
60 | return pretty
|
61 |
|
62 | function pretty (inputData) {
|
63 | let log
|
64 | if (!isObject(inputData)) {
|
65 | const parsed = jsonParser(inputData)
|
66 | if (parsed.err || !isObject(parsed.value)) {
|
67 |
|
68 | return inputData + EOL
|
69 | }
|
70 | log = parsed.value
|
71 | } else {
|
72 | log = inputData
|
73 | }
|
74 |
|
75 | if (search && !jmespath.search(log, search)) {
|
76 | return
|
77 | }
|
78 |
|
79 | const prettifiedMessage = prettifyMessage({ log, messageKey, colorizer, messageFormat, levelLabel })
|
80 |
|
81 | if (ignoreKeys) {
|
82 | log = Object.keys(log)
|
83 | .filter(key => !ignoreKeys.has(key))
|
84 | .reduce((res, key) => {
|
85 | res[key] = log[key]
|
86 | return res
|
87 | }, {})
|
88 | }
|
89 |
|
90 | const prettifiedLevel = prettifyLevel({ log, colorizer, levelKey })
|
91 | const prettifiedMetadata = prettifyMetadata({ log })
|
92 | const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey })
|
93 |
|
94 | let line = ''
|
95 | if (opts.levelFirst && prettifiedLevel) {
|
96 | line = `${prettifiedLevel}`
|
97 | }
|
98 |
|
99 | if (prettifiedTime && line === '') {
|
100 | line = `${prettifiedTime}`
|
101 | } else if (prettifiedTime) {
|
102 | line = `${line} ${prettifiedTime}`
|
103 | }
|
104 |
|
105 | if (!opts.levelFirst && prettifiedLevel) {
|
106 | if (line.length > 0) {
|
107 | line = `${line} ${prettifiedLevel}`
|
108 | } else {
|
109 | line = prettifiedLevel
|
110 | }
|
111 | }
|
112 |
|
113 | if (prettifiedMetadata) {
|
114 | if (line.length > 0) {
|
115 | line = `${line} ${prettifiedMetadata}:`
|
116 | } else {
|
117 | line = prettifiedMetadata
|
118 | }
|
119 | }
|
120 |
|
121 | if (line.endsWith(':') === false && line !== '') {
|
122 | line += ':'
|
123 | }
|
124 |
|
125 | if (prettifiedMessage) {
|
126 | if (line.length > 0) {
|
127 | line = `${line} ${prettifiedMessage}`
|
128 | } else {
|
129 | line = prettifiedMessage
|
130 | }
|
131 | }
|
132 |
|
133 | if (line.length > 0) {
|
134 | line += EOL
|
135 | }
|
136 |
|
137 | if (log.type === 'Error' && log.stack) {
|
138 | const prettifiedErrorLog = prettifyErrorLog({
|
139 | log,
|
140 | errorLikeKeys: errorLikeObjectKeys,
|
141 | errorProperties: errorProps,
|
142 | ident: IDENT,
|
143 | eol: EOL
|
144 | })
|
145 | line += prettifiedErrorLog
|
146 | } else if (!hideObject) {
|
147 | const skipKeys = [messageKey, levelKey, timestampKey].filter(key => typeof log[key] === 'string' || typeof log[key] === 'number')
|
148 | const prettifiedObject = prettifyObject({
|
149 | input: log,
|
150 | skipKeys,
|
151 | customPrettifiers,
|
152 | errorLikeKeys: errorLikeObjectKeys,
|
153 | eol: EOL,
|
154 | ident: IDENT
|
155 | })
|
156 | line += prettifiedObject
|
157 | }
|
158 |
|
159 | return line
|
160 | }
|
161 | }
|