forked from bioform/co-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.63 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
var fs = require('fs');
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true, colorize: true }),
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true, colorize: true }),
],
exitOnError: false
});
// remove compound colorization for file transport
old_log = winston.transports.File.prototype.log;
winston.transports.File.prototype.log = function (level, msg, meta, callback) {
msg = msg.replace(/\u001b\[\d{1,3}m/g, '');
old_log.call(this, level, msg, meta, callback);
};
// add "write" method for compatibility with old compound logger
logger.write = function(str){
this.info(str);
}
// init logger with compound
logger.init = function(compound){
if( !compound || !compound.root || !compound.app || typeof compound.app.set !== 'function'){
throw "'compound' object should be defined";
}
compound.logger = this;
var logsDir = compound.root + '/log';
var logFile = compound.app.set('env') + '.log'
var addTransport = function(){
this.add(winston.transports.File, {
filename: logsDir + '/' + logFile,
handleExceptions: true,
json: false
});
}.bind(this);
try {
if ( !fs.existsSync(logsDir) ){
fs.mkdirSync( logsDir );
addTransport();
}
else {
stats = fs.statSync( logsDir );
if ( !stats.isDirectory() ){
this.error('Cannot create log directory. File with the same name is already exists');
}
else {
// Directory is already exists
addTransport();
}
}
}
catch( err ){
this.error('Cannot create log directory. ' + err);
}
};
module.exports = logger;