This repository was archived by the owner on Nov 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogr.js
More file actions
72 lines (54 loc) · 1.89 KB
/
logr.js
File metadata and controls
72 lines (54 loc) · 1.89 KB
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
'use strict';
const
BUNY = process.env.EM_LOGR_BUNYAN_DEFAULT_NAME || process.env.EM_LOGR_DEFAULT_NAME || 'BUNY',
PINO = process.env.EM_LOGR_PINOJS_DEFAULT_NAME || process.env.EM_LOGR_DEFAULT_NAME || 'PINO';
function overrideBunyanChildMethod(bunyan) {
bunyan.prototype.child = function (options, simple) {
var name;
if (options && options.name) {
name = options.name;delete options.name;
}
const child = new (this.constructor)(this, options || {}, simple);
if (name) child.fields.name = name;
return child;
};
bunyan.__childMethodReplaced = true;
}
function getBunyan(_options = {}) {
const bunyan = require('bunyan');
if (!bunyan.__childMethodReplaced) overrideBunyanChildMethod(bunyan);
const options = Object.assign({
name: BUNY,
level,
serializers: {
req: bunyan.stdSerializers.req,
res: bunyan.stdSerializers.res
},
src: ( ['debug','trace'].indexOf(level) !== -1 ),
}, _options);
const logr = bunyan.createLogger(options);
return Object.defineProperty(logr, 'name', {
get( ){ return this.fields.name; },
set(_){ return this.fields.name = _; }
});
}
function getPino(options = {}) {
const pino = require('pino')(Object.assign({name: PINO, level}, options));
pino.fields = {pino};
Object.defineProperty(pino.fields, 'name', {
get( ){ return this.pino.name; },
set(_){ return this.pino.name = _; }
});
return pino;
}
const level = !process.env.LOG_LEVEL ? 'debug' :
(process.env.LOG_LEVEL === 'test' ? 'fatal' : process.env.LOG_LEVEL);
const create = (options={}, _level) => {
const logr = ['debug', 'trace'].includes(_level || level) ? getBunyan(options) : getPino(options);
logr.create = create;
return logr;
};
// const logr = ['debug', 'trace'].includes(level) ? getBunyan(level) : getPino(level);
const logr = create();
logr.raw = (_) => process.stdout.write(_ + '\n');
module.exports = logr;