我已经将Winston设置为记录器,并且效果很好。 但是,我有几个要登录到不同文件的过程。如何将文件名传递到记录器实例?
我将其用作街区
'use strict';
const { createLogger, format, transports } = require('winston');
require('winston-daily-rotate-file');
const fs = require('fs');
const path = require('path');
const logDir = '/codeforge/log';
const dailyRotateFileTransport = new transports.DailyRotateFile({
filename: `${logDir}/%DATE%-engine.log`,
maxSize: "1g",
maxDays: "3d",
zippedArchive: true,
datePattern: 'YYYY-MM-DD'
});
const logger = createLogger({
// change level if in dev environment versus production
level: 'debug',
maxsize: '500m',
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
// for the log file
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: [
new transports.Console({
level: 'info',
format: format.combine(
format.colorize(),
format.printf(
info => `${info.message}`
)
)
}),
dailyRotateFileTransport
]
});
module.exports = logger
我想传递一个文件名来替换“ -engine”部分来为其创建或附加日志文件。
感谢任何提示。
答案 0 :(得分:0)
使记录器进入您调用的功能
const log = require('logger')('myfilename');
代码:
'use strict';
const { createLogger, format, transports } = require('winston');
require('winston-daily-rotate-file');
const fs = require('fs');
const path = require('path');
const logDir = '/codeforge/log';
const dailyRotateFileTransport = filename => new transports.DailyRotateFile({
filename: `${logDir}/%DATE%-${filename}.log`,
maxSize: "1g",
maxDays: "3d",
zippedArchive: true,
datePattern: 'YYYY-MM-DD'
});
const logger = function(filename){
return createLogger({
// change level if in dev environment versus production
level: 'debug',
maxsize: '500m',
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
// for the log file
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: [
new transports.Console({
level: 'info',
format: format.combine(
format.colorize(),
format.printf(
info => `${info.message}`
)
)
}),
dailyRotateFileTransport(fileName)
]
});
}
module.exports = logger // is now a function
答案 1 :(得分:0)
我不想多次创建或重新初始化Logger实例,我们可以通过运行来检查
DEBUG=winston* yarn start
我正在使用 child logger ,这是我的打字稿记录器,
import winston from 'winston';
const { transports, format } = winston;
const { LOG_LEVEL, APP_ENV } = process.env;
const logFormatter = APP_ENV === 'dev'
? format.combine(
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf((meta: any) => {
const { level, message, timestamp, namespace, stack, ...restMeta } = meta;
const displayNamespace = namespace ? `[${namespace}] -` : '';
const stackMessage = stack ? `\n${stack}` : '';
const otherMetaMessage = Object.keys(restMeta).length > 0 ? `\n${JSON.stringify(restMeta)}` : '';
return `${timestamp} ${displayNamespace} ${level}: ${message} ${otherMetaMessage}${stackMessage}`;
})
)
: format.combine(winston.format.timestamp(), winston.format.json());
const logger = winston.createLogger({
level: LOG_LEVEL || 'debug',
format: logFormatter,
transports: [
new winston.transports.Console({
silent: APP_ENV === 'test',
}),
],
});
export const Logger = (namespace?: string) => {
if (namespace) {
return logger.child({ namespace });
}
return logger;
};
用法: service-abc.js
const LOGGER = Logger('service:abc');
export class serviceAbc{
doSomething(){
LOGGER.info("do something");
}
}