intval3/lib/log/index.js

29 lines
934 B
JavaScript
Raw Normal View History

2017-09-25 02:27:53 +00:00
'use strict'
const { transports, format, createLogger } = require('winston')
2017-09-25 02:27:53 +00:00
/**
* createLog() - Returns a winston logger configured to service
*
* @param {string} label Label appearing on logger
* @param {string} filename Optional file to write log to
* @returns {object} Winston logger
*/
function createLog (label, filename = null) {
const transportsArr = [ new (transports.Console)({ label : label }) ]
2017-09-25 02:27:53 +00:00
if (filename !== null) {
transportsArr.push( new (transports.File)({ label : label, filename : filename }) )
2017-09-25 02:27:53 +00:00
}
2020-01-14 18:33:13 +00:00
return new (createLogger)({
format: format.combine(
2020-01-14 18:34:31 +00:00
format.label({ label : label || 'intval3' }),
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf((info) => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`+(info.splat!==undefined?`${info.splat}`:" "))
),
transports: transportsArr
2017-09-25 02:27:53 +00:00
})
}
module.exports = createLog