-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (99 loc) · 2.43 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
'use strict';
/****************************
* Modules
****************************/
var winston = require('winston');
var objectAssign = require('object-assign');
var util = require('./util');
/****************************
* Constants
****************************/
var ENV = process.env.NODE_ENV || '';
var DEFAULT_CONFIG = {
syslogEnvs: ['staging', 'production']
};
// Setup winston
require('winston-syslog').SysLog;
winston.remove(winston.transports.Console);
/****************************
* Export
****************************/
module.exports = function(moduleConfig) {
console.log('Winston says process.env.NODE_ENV:', ENV);
var config = objectAssign(DEFAULT_CONFIG, moduleConfig);
var logger, sysLogger, log;
// setup default transports
var transports = [
new winston.transports.Console({
handleExceptions: false,
json: false,
colorize: true,
timestamp: true,
level: 'debug'
})
];
// winston config
var winstonConfig = {
levels: {
silly: 0,
debug: 2,
verbose: 3,
info: 4,
warn: 5,
error: 6
},
colors: {
silly: 'magenta',
debug: 'blue',
verbose: 'cyan',
info: 'green',
warn: 'yellow',
error: 'red'
}
};
// setup syslog transport and format the message
if(config.syslog){
config.syslog.timestamp = util.getTimeStamp;
sysLogger = new winston.transports.Syslog(config.syslog);
log = sysLogger.log;
sysLogger.log = function(level, msg, meta, callback){
log.call(
sysLogger,
util.getSyslogLevel(level),
util.getSyslogMsg(msg),
meta,
callback
);
}
}
// add syslog transport to logger
if (sysLogger && config.syslogEnvs.indexOf(ENV) >= 0) {
transports.push(sysLogger);
}
// create new winston logger
logger = new winston.Logger({
transports: transports,
colors: winstonConfig.colors
});
// override default console functions
console.silly = function(){
logger.silly.apply(logger, util.formatArgs(arguments));
};
console.log = function(){
logger.verbose.apply(logger, util.formatArgs(arguments));
};
console.info = function(){
logger.info.apply(logger, util.formatArgs(arguments));
};
console.warn = function(){
logger.warn.apply(logger, util.formatArgs(arguments));
};
console.debug = function(){
logger.debug.apply(logger, util.formatArgs(arguments));
};
console.error = function(){
logger.error.apply(logger, util.formatArgs(arguments));
};
// return the instance of winston to do stuff with if needed
return logger;
};