forked from wojtkowiak/meteor-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggerManager.js
82 lines (71 loc) · 2.56 KB
/
loggerManager.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
import { join } from 'path';
import winston from 'winston';
export default class LoggerManager {
/**
* @param {App} $ - context.
*/
constructor($) {
this.$ = $;
/* TODO: fix `Possible EventEmitter memory leak detected.` warning - will probably have to
drop winston in favor of bunyan (winston does not seem to be actively supported)
*/
// Default Winston transports.
this.fileLogConfiguration = {
level: 'debug',
filename: join($.userDataDir, 'run.log'),
handleExceptions: false,
json: false,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false
};
this.consoleLogConfiguration = {
level: 'debug',
handleExceptions: false,
json: false,
colorize: true
};
this.loggerTransports = [
new (winston.transports.Console)(this.consoleLogConfiguration),
new (winston.transports.File)(this.fileLogConfiguration)
];
winston.loggers.options.transports = this.loggerTransports;
this.mainLogger = this.configureLogger();
}
/**
* @returns {Log}
*/
getMainLogger() {
return this.mainLogger;
}
/**
* Returns a new logger instance.
* @param {string} entityName
* @returns {Log}
*/
configureLogger(entityName = 'main') {
winston.loggers.add(entityName, {});
const logger = winston.loggers.get(entityName);
if (entityName !== 'main') {
logger.add(winston.transports.File, {
level: 'debug',
name: entityName,
handleExceptions: false,
filename: join(this.$.userDataDir, `${entityName}.log`)
});
}
logger.filters.push((level, msg) => `[${entityName}] ${msg}`);
logger.entityName = entityName;
logger.getLoggerFor = (subEntityName) => {
if (!winston.loggers.loggers[`${logger.entityName}__${subEntityName}`]) {
winston.loggers.add(`${logger.entityName}__${subEntityName}`, {});
const newLogger = winston.loggers.get(`${logger.entityName}__${subEntityName}`);
newLogger.filters.push((level, msg) => `[${logger.entityName}] [${subEntityName}] ${msg}`);
newLogger.getLoggerFor = logger.getLoggerFor;
return newLogger;
}
return winston.loggers.get(`${logger.entityName}__${subEntityName}`);
};
return logger;
}
}