forked from FlexLabs/Dyno-datafactory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
85 lines (73 loc) · 1.83 KB
/
Copy pathlogger.js
File metadata and controls
85 lines (73 loc) · 1.83 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
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const util = require('util');
const moment = require('moment');
const winston = require('winston');
const Sentry = require('winston-sentry');
/**
* @class Logger
*/
class Logger {
/**
* @prop {Array} transports
* @prop {Boolean} exitOnError
*/
constructor(options) {
this._options = options;
this.transports = [
new (winston.transports.Console)({
colorize: true,
level: options.logLevel || 'info',
debugStdout: true,
// handleExceptions: true,
// humanReadableUnhandledException: true,
timestamp: () => new Date(),
formatter: this._formatter.bind(this),
}),
];
if (options.sentry && options.sentry.dsn) {
this.transports.push(new Sentry({
level: options.sentry.logLevel || 'error',
dsn: options.sentry.dsn,
}));
}
this.exitOnError = false;
return new (winston.Logger)(this);
}
/**
* Custom formatter for console
* @param {Object} options Formatter options
* @returns {String}
* @private
*/
_formatter(options) {
let ts = util.format('[%s]', moment(options.timestamp()).format('HH:mm:ss')),
level = winston.config.colorize(options.level);
if (this._options.hasOwnProperty('shardId')) {
ts = `[Shard ${this._options.shardId}] ${ts}`;
}
switch (options.level) {
case 'debug':
ts += ' ⚙ ';
break;
case 'info':
ts += ' 🆗 ';
break;
case 'error':
ts += ' 🔥 ';
break;
case 'warn':
ts += ' ☣ ';
break;
case 'silly':
ts += ' 💩 ';
break;
}
let message = ts + ' ' + level + ': ' + (undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t' + util.inspect(options.meta) : '');
if (options.colorize === 'all') {
return winston.config.colorize(options.level, message);
}
return message;
}
}
module.exports = Logger;