This repository has been archived by the owner on Jul 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (151 loc) · 4.96 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
const path = require('path');
const format = require('util').format;
const fs = require('fs.extra');
const winston = require('winston');
/**
* Enum logging level values.
* @enum {String}
*/
const ENUM_LEVELS = { // eslint-disable-line no-unused-vars
trace: 'The highest level of logging, logs everything.',
debug: 'Less spammy than trace, includes most info relevant for debugging.',
info: 'The default logging level. Logs useful info, warnings, and errors.',
warn: 'Only logs warnings and errors.',
error: 'Only logs errors.'
};
/**
* A factory that configures and returns a Logger constructor.
* @param [initialOpts] {Object} - Configuration for the logger.
*
* @param [initialOpts.console] {Object} - Configuration for the console logging.
* @param [initialOpts.console.enabled=false] {Boolean} - Whether to enable console logging.
* @param [initialOpts.console.level="info"] {ENUM_LEVELS} - The level of logging to output to the console.
*
* @param [initialOpts.file] {Object} - Configuration for file logging.
* @param initialOpts.file.path {String} - Where the log file should be saved.
* @param [initialOpts.file.enabled=false] {Boolean} - Whether to enable file logging.
* @param [initialOpts.file.level="info"] {ENUM_LEVELS} - The level of logging to output to file.
*
* @param [initialOpts.replicants=false] {Boolean} - Whether to enable logging specifically for the Replicants system.
*
* @param [rollbar] {Object} - A pre-configured server-side Rollbar npm package instance.
*
* @returns {function} - A constructor used to create discrete logger instances.
*/
module.exports = function (initialOpts, rollbar) {
initialOpts = initialOpts || {};
initialOpts.console = initialOpts.console || {};
initialOpts.file = initialOpts.file || {};
initialOpts.file.path = initialOpts.file.path || 'logs/nodecg.log';
const consoleTransport = new winston.transports.Console({
name: 'nodecgConsole',
prettyPrint: true,
colorize: true,
level: initialOpts.console.level || 'info',
silent: !initialOpts.console.enabled,
stderrLevels: ['warn', 'error']
});
const fileTransport = new winston.transports.File({
name: 'nodecgFile',
json: false,
prettyPrint: true,
filename: initialOpts.file.path,
level: initialOpts.file.level || 'info',
silent: !initialOpts.file.enabled
});
winston.addColors({
trace: 'green',
debug: 'cyan',
info: 'white',
warn: 'yellow',
error: 'red'
});
const mainLogger = new (winston.Logger)({
transports: [consoleTransport, fileTransport],
levels: {
trace: 4,
debug: 3,
info: 2,
warn: 1,
error: 0
}
});
/**
* Constructs a new Logger instance that prefixes all output with the given name.
* @param name {String} - The label to prefix all output of this logger with.
* @returns {Object} - A Logger instance.
* @constructor
*/
class Logger {
constructor(name) {
this.name = name;
}
trace() {
arguments[0] = '[' + this.name + '] ' + arguments[0];
mainLogger.trace.apply(mainLogger, arguments);
}
debug() {
arguments[0] = '[' + this.name + '] ' + arguments[0];
mainLogger.debug.apply(mainLogger, arguments);
}
info() {
arguments[0] = '[' + this.name + '] ' + arguments[0];
mainLogger.info.apply(mainLogger, arguments);
}
warn() {
arguments[0] = '[' + this.name + '] ' + arguments[0];
mainLogger.warn.apply(mainLogger, arguments);
}
error() {
arguments[0] = '[' + this.name + '] ' + arguments[0];
mainLogger.error.apply(mainLogger, arguments);
if (rollbar) {
rollbar.reportMessage(format(...arguments), 'error');
}
}
replicants() {
if (!Logger._shouldLogReplicants) {
return;
}
arguments[0] = '[' + this.name + '] ' + arguments[0];
mainLogger.info.apply(mainLogger, arguments);
}
static globalReconfigure(opts) {
_configure(opts);
}
}
Logger._winston = mainLogger;
// A messy bit of internal state used to determine if the special-case "replicants" logging level is active.
Logger._shouldLogReplicants = Boolean(initialOpts.replicants);
_configure(initialOpts);
function _configure(opts) {
// Initialize opts with empty objects, if nothing was provided.
opts = opts || {};
opts.console = opts.console || {};
opts.file = opts.file || {};
if (typeof opts.console.enabled !== 'undefined') {
consoleTransport.silent = !opts.console.enabled;
}
if (typeof opts.console.level !== 'undefined') {
consoleTransport.level = opts.console.level;
}
if (typeof opts.file.enabled !== 'undefined') {
fileTransport.silent = !opts.file.enabled;
}
if (typeof opts.file.level !== 'undefined') {
fileTransport.level = opts.file.level;
}
if (typeof opts.file.path !== 'undefined') {
fileTransport.filename = opts.file.path;
// Make logs folder if it does not exist.
if (!fs.existsSync(path.dirname(opts.file.path))) {
fs.mkdirpSync(path.dirname(opts.file.path));
}
}
if (typeof opts.replicants !== 'undefined') {
Logger._shouldLogReplicants = opts.replicants;
}
}
return Logger;
};