This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (60 loc) · 1.56 KB
/
index.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const logfmt = require('logfmt')
const { Console } = require('console')
function plainfy ({ object = {}, parentKeys = [] }) {
let plainObject = {}
Object.keys(object).forEach(key => {
if (typeof object[key] !== 'object') {
if (parentKeys.length > 0) {
plainObject[`${parentKeys.join('_')}_${key}`] = object[key]
return
}
plainObject[key] = object[key]
return
}
parentKeys.push(key)
plainObject = plainfy({ parentKeys, object: object[key] })
})
return plainObject
}
function format (params) {
const data = params.reduce(
(data, param) => {
if (typeof param === 'string') {
if (data.msg) {
data.msg += ` ${param}`
} else {
data.msg = param
}
}
if (typeof param === 'object' && param !== null) {
data = Object.assign(data, plainfy({ object: param }))
}
return data
},
{ level: 'info' }
)
return logfmt.stringify(data)
}
function ConsoleLogFmt (options) {
const original = new Console(
Object.assign({ stdout: process.stderr, stderr: process.stderr }, options)
)
return {
log: (...params) => {
const string = format(params)
original.log(string)
return string
},
warn: (...params) => {
const string = format([...params, { level: 'warn' }])
original.warn(string)
return string
},
error: (...params) => {
const string = format([...params, { level: 'error' }])
original.error(string)
return string
}
}
}
module.exports = ConsoleLogFmt