epg-grabber/src/logger.js

35 lines
823 B
JavaScript
Raw Normal View History

2022-06-11 20:01:39 +02:00
const { createLogger, format, transports } = require('winston')
const { combine, timestamp, printf } = format
2022-06-15 15:48:10 +02:00
const path = require('path')
2022-06-11 20:01:39 +02:00
module.exports.create = create
function create(options) {
const fileFormat = printf(({ level, message, timestamp }) => {
return `[${timestamp}] ${level.toUpperCase()}: ${message}`
})
2022-06-15 15:48:10 +02:00
const consoleFormat = printf(({ level, message }) => {
2022-06-11 20:01:39 +02:00
if (level === 'error') return ` Error: ${message}`
return message
})
const t = [new transports.Console({ format: consoleFormat })]
if (options.log) {
t.push(
new transports.File({
filename: path.resolve(options.log),
format: combine(timestamp(), fileFormat),
options: { flags: 'w' }
})
)
}
return createLogger({
level: options.logLevel,
transports: t
})
}