Last active
January 23, 2020 08:36
-
-
Save darkcolonist/877e9f5d9cc82b3428b98f374b4a4351 to your computer and use it in GitHub Desktop.
winston logger implementation in node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* dependencies | |
* "moment-timezone": "^0.5.23", | |
* "winston": "^3.2.1" | |
*/ | |
const moment = require('moment-timezone'); | |
const {transports, createLogger, format} = require('winston'); | |
var util = { | |
_loggers: {}, | |
/** | |
* like the above but instead, this logs to file | |
* | |
* sample usage: | |
* | |
* util.logf('debug-leakance','example-'+util.randstring(4, util.rand(5,16))); | |
* util.logf('debug-duodecimo','example-'+util.randstring(4, util.rand(5,16))); | |
* util.logf('debug-undefied','example-'+util.randstring(4, util.rand(5,16))); | |
* util.logf('debug-burring','example-'+util.randstring(4, util.rand(5,16))); | |
* util.logf('debug-lux','example-'+util.randstring(4, util.rand(5,16))); | |
*/ | |
logf: function(type, message, level){ | |
var level = level === undefined ? "info" : level; | |
if(Array.isArray(message)) | |
message = JSON.stringify(message) | |
else if(typeof message === 'object') | |
message = JSON.stringify(message) | |
var theLogger = null; | |
if(util._loggers[type] == undefined){ | |
theLogger = createLogger({ | |
format: format.combine( | |
format.printf(info => util.winstonMomentWrapper() +` ${info.level}: ${info.message}`+(info.splat!==undefined?`${info.splat}`:" ")) | |
), | |
transports: [ | |
new (transports.File)({ | |
filename: `./traces/${type}.log`, | |
json: false, | |
maxsize: 10000000, // 10MB | |
maxFiles: 1 | |
}) | |
] | |
}); | |
util._loggers[type] = theLogger; | |
}else{ | |
theLogger = util._loggers[type]; | |
} | |
theLogger.log(level, message); | |
}, | |
winstonMomentWrapper: function(){ | |
return util.moment(null, { | |
format: "YYYY-MMM-DD h:mm:ss.SA", | |
timezone: "Asia/Manila" | |
}); | |
}, | |
/** | |
* get a moment instance pre-configured | |
* @param string datetime nullable | |
* @param Object customParams | |
* { | |
* // null if you prefer application timezone | |
* timezone: "America/Los_Angeles", | |
* | |
* // null if you prefer the default format of momentJS | |
* format: "MMMM Do YYYY, h:mm:ss a", | |
* } | |
* @return {[type]} [description] | |
*/ | |
moment: function(datetime, customParams){ | |
if(datetime === null) | |
datetime = undefined; | |
var myMoment = moment(datetime); | |
if(customParams === undefined) | |
customParams = {}; | |
if(customParams.timezone !== undefined){ | |
myMoment.tz(customParams.timezone); | |
}else{ | |
myMoment.tz(config.app.timezone); | |
} | |
if(customParams.format !== undefined){ | |
return myMoment.format(customParams.format); | |
}else{ | |
return myMoment; | |
} | |
} | |
} | |
module.exports = util; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment