This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
/
logger.js
108 lines (97 loc) · 3.6 KB
/
logger.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
'use strict';
const levels = { error : 5, warn : 4, info : 3, debug : 2 };
/**
Generates Splunk consumable logs in node 4.3.2 AWS Lambdas.
### Install
```
npm install --save @seek/splunk-logger
```
### Usage
Provides the usual {@link Logger#error}, {@link Logger#warn}, {@link Logger#info}, and {@link Logger#debug}
Log messages are formatted like this where the `name` component will only be used if given in the logger's constructor :
```
name=<name>, level=<level>, msg=<msg>
```
*/
class Logger {
/**
* Create a logger
*
* @param {string} [name] Optional name to give the logger. This will be added to all log bodies.
* @param {string} [level=info] Optional minimum level to output. Defaults to `info`
*/
constructor(name, level) {
level = level || 'info'
// 'name' will be used as the third keyval pair of the log string... if given
this.name = name
this.formattedName = name ? `, name=${JSON.stringify(name)}` : '';
this.setLevel(level);
}
/**
* Set the minimum log level to output
*
* @param {string} level Should be one of `error`, `warn`, `info`, `debug`
* @return {Logger} this Logger instance
*/
setLevel(level) {
if (!levels[level]) {
throw new TypeError(`unknown error level ${level}`);
}
this.level = levels[level];
return this;
}
/**
* Emit a log message at the given level
*
* @param {*} msg Thing to be logged. If given an object literal it will be appended to the log in the format `<code>key1=value1, key2=value2</code>`
* @param {string} level log level to be used in `error`, `warn`, `info`, `debug`. Defaults to `info`.
*/
log(msg, level) {
if (!level || typeof level !== 'string' || !levels[level]) {
throw new TypeError(`Level must be one of [${levels.keys.join(', ')}].`)
}
let keyVals = {}
if (levels[level] >= this.level) {
if (Object.prototype.toString.call(msg) === '[object Object]') {
keyVals = msg
} else {
keyVals = { msg: `${msg}`}
}
// Append all the items in the object to the output string in the desired format.
console.log(Object.keys(keyVals).reduce((acc, key) => {
if (key === 'name' || key === 'level' || key === 'time'){
return acc; //ignore
}
else {
let val = keyVals[key]
val = Object.prototype.toString.call(val) === '[object Object]' ? val : `${val}`;
// Append and escape/quote
return `${acc}, ${key}=${JSON.stringify(val)}`;
}
// This might look a bit gnarly -> it's the initial value for the reduce function's accumulator
// i.e the first component of our log string!
}, `time=${new Date().toISOString()}, level=${level}${this.formattedName}`));
}
}
/**
* Emit error level log
* @param {*} msg Thing to be logged. See {@link log}
*/
error(msg) { this.log(msg, 'error'); }
/**
* Emit warn level log
* @param {*} msg Thing to be logged. See {@link log}
*/
warn(msg) { this.log(msg, 'warn'); }
/**
* Emit info level log
* @param {*} msg Thing to be logged. See {@link log}
*/
info(msg) { this.log(msg, 'info'); }
/**
* Emit debug level log
* @param {*} msg Thing to be logged. See {@link log}
*/
debug(msg) { this.log(msg, 'debug'); }
}
module.exports = Logger;