-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathindex.js
146 lines (119 loc) · 3.21 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const CorrelationIds = require('@dazn/lambda-powertools-correlation-ids')
// Levels here are identical to bunyan practices
// https://github.com/trentm/node-bunyan#levels
const LogLevels = {
DEBUG: 20,
INFO: 30,
WARN: 40,
ERROR: 50
}
// most of these are available through the Node.js execution environment for Lambda
// see https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
const DEFAULT_CONTEXT = {
awsRegion: process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION,
functionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
functionVersion: process.env.AWS_LAMBDA_FUNCTION_VERSION,
functionMemorySize: process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE,
environment: process.env.ENVIRONMENT || process.env.STAGE // convention in our functions
}
class Logger {
constructor ({
correlationIds = CorrelationIds,
level = process.env.LOG_LEVEL
} = {}) {
this.correlationIds = correlationIds
this.level = (level || 'DEBUG').toUpperCase()
this.originalLevel = this.level
if (correlationIds.debugEnabled) {
this.enableDebug()
}
}
get context () {
return {
...DEFAULT_CONTEXT,
...this.correlationIds.get()
}
}
isEnabled (level) {
return level >= (LogLevels[this.level] || LogLevels.DEBUG)
}
appendError (params, err) {
if (!err) {
return params
}
return {
...params || {},
errorName: err.name,
errorMessage: err.message,
stackTrace: err.stack
}
}
log (levelName, message, params) {
const level = LogLevels[levelName]
if (!this.isEnabled(level)) {
return
}
const logMsg = {
...this.context,
...params,
level,
sLevel: levelName,
message
}
const consoleMethods = {
DEBUG: console.debug,
INFO: console.info,
WARN: console.warn,
ERROR: console.error
}
// re-order message and params to appear earlier in the log row
consoleMethods[levelName](JSON.stringify({ message, ...params, ...logMsg }, (key, value) => typeof value === 'bigint'
? value.toString()
: value
))
}
debug (msg, params) {
this.log('DEBUG', msg, params)
}
info (msg, params) {
this.log('INFO', msg, params)
}
warn (msg, params, err) {
const parameters = !err && params instanceof Error ? this.appendError({}, params) : this.appendError(params, err)
this.log('WARN', msg, parameters)
}
error (msg, params, err) {
const parameters = !err && params instanceof Error ? this.appendError({}, params) : this.appendError(params, err)
this.log('ERROR', msg, parameters)
}
enableDebug () {
this.level = 'DEBUG'
return () => this.resetLevel()
}
resetLevel () {
this.level = this.originalLevel
}
static debug (...args) {
globalLogger.debug(...args)
}
static info (...args) {
globalLogger.info(...args)
}
static warn (...args) {
globalLogger.warn(...args)
}
static error (...args) {
globalLogger.error(...args)
}
static enableDebug () {
return globalLogger.enableDebug()
}
static resetLevel () {
globalLogger.resetLevel()
}
static get level () {
return globalLogger.level
}
}
const globalLogger = new Logger()
module.exports = Logger