- Allow setting labels via new createLogger method - Make logger initialization more robustpull/251/head
parent
ab631fa26e
commit
42fe535df7
@ -1,80 +1,88 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
|
import { format as utilFormat } from "node:util"
|
||||||
|
|
||||||
import winston from "winston";
|
import winston from "winston";
|
||||||
|
|
||||||
const configPath = join(process.cwd(), "config");
|
let winstonLogger;
|
||||||
|
|
||||||
function messageFormatter(logInfo) {
|
function init() {
|
||||||
if (logInfo.stack) {
|
const configPath = join(process.cwd(), "config");
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
|
||||||
}
|
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const consoleFormat = winston.format.combine(
|
|
||||||
winston.format.errors({ stack: true }),
|
|
||||||
winston.format.splat(),
|
|
||||||
winston.format.timestamp(),
|
|
||||||
winston.format.colorize(),
|
|
||||||
winston.format.printf(messageFormatter)
|
|
||||||
);
|
|
||||||
|
|
||||||
const fileFormat = winston.format.combine(
|
|
||||||
winston.format.errors({ stack: true }),
|
|
||||||
winston.format.splat(),
|
|
||||||
winston.format.timestamp(),
|
|
||||||
winston.format.printf(messageFormatter)
|
|
||||||
);
|
|
||||||
|
|
||||||
const logger = winston.createLogger({
|
function combineMessageAndSplat() {
|
||||||
level: process.env.LOG_LEVEL || 'info',
|
return {
|
||||||
transports: [
|
// eslint-disable-next-line no-unused-vars
|
||||||
new winston.transports.Console({
|
transform: (info, opts) => {
|
||||||
format: consoleFormat,
|
// combine message and args if any
|
||||||
handleExceptions: true,
|
// eslint-disable-next-line no-param-reassign
|
||||||
handleRejections: true
|
info.message = utilFormat(info.message, ...info[Symbol.for('splat')] || []);
|
||||||
}),
|
return info;
|
||||||
|
}
|
||||||
new winston.transports.File({
|
}
|
||||||
format: fileFormat,
|
}
|
||||||
filename: `${configPath}/logs/homepage.log`,
|
|
||||||
handleExceptions: true,
|
|
||||||
handleRejections: true
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
function debug(message, ...args) {
|
|
||||||
logger.debug(message, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
function verbose(message, ...args) {
|
function messageFormatter(logInfo) {
|
||||||
logger.verbose(message, ...args);
|
if (logInfo.label) {
|
||||||
}
|
if (logInfo.stack) {
|
||||||
|
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.stack}`;
|
||||||
|
}
|
||||||
|
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.message}`;
|
||||||
|
}
|
||||||
|
|
||||||
function info(message, ...args) {
|
if (logInfo.stack) {
|
||||||
logger.info(message, ...args);
|
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
||||||
}
|
}
|
||||||
|
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
||||||
|
};
|
||||||
|
|
||||||
function warn(message, ...args) {
|
winstonLogger = winston.createLogger({
|
||||||
logger.warn(message, ...args);
|
level: process.env.LOG_LEVEL || 'info',
|
||||||
}
|
transports: [
|
||||||
|
new winston.transports.Console({
|
||||||
|
format: winston.format.combine(
|
||||||
|
winston.format.errors({ stack: true}),
|
||||||
|
combineMessageAndSplat(),
|
||||||
|
winston.format.timestamp(),
|
||||||
|
winston.format.colorize(),
|
||||||
|
winston.format.printf(messageFormatter)
|
||||||
|
),
|
||||||
|
handleExceptions: true,
|
||||||
|
handleRejections: true
|
||||||
|
}),
|
||||||
|
|
||||||
function error(message, ...args) {
|
new winston.transports.File({
|
||||||
logger.error(message, ...args);
|
format: winston.format.combine(
|
||||||
}
|
winston.format.errors({ stack: true}),
|
||||||
|
combineMessageAndSplat(),
|
||||||
|
winston.format.timestamp(),
|
||||||
|
winston.format.printf(messageFormatter)
|
||||||
|
),
|
||||||
|
filename: `${configPath}/logs/homepage.log`,
|
||||||
|
handleExceptions: true,
|
||||||
|
handleRejections: true
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
function crit(message, ...args) {
|
// patch the console log mechanism to use our logger
|
||||||
logger.crit(message, ...args);
|
const consoleMethods = ['log', 'debug', 'info', 'warn', 'error']
|
||||||
|
consoleMethods.forEach(method => {
|
||||||
|
// workaround for https://github.com/winstonjs/winston/issues/1591
|
||||||
|
switch (method) {
|
||||||
|
case 'log':
|
||||||
|
console[method] = winstonLogger.info.bind(winstonLogger);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console[method] = winstonLogger[method].bind(winstonLogger);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const thisModule = {
|
export default function createLogger(label) {
|
||||||
debug,
|
if (!winstonLogger) {
|
||||||
verbose,
|
init();
|
||||||
info,
|
}
|
||||||
warn,
|
|
||||||
error,
|
|
||||||
crit
|
|
||||||
};
|
|
||||||
|
|
||||||
export default thisModule;
|
return winstonLogger.child({ label });
|
||||||
|
}
|
Loading…
Reference in new issue