gpt4 book ai didi

javascript - winston 的多个日志文件具有最大大小和每日轮换

转载 作者:行者123 更新时间:2023-12-01 02:55:27 28 4
gpt4 key购买 nike

现在我使用 Winston 来记录最大大小和每日轮换

我希望每个 API 端点都有一个文件,从而定义多个日志文件

我怎样才能实现这一目标?

这是我的 winston 助手类

import * as winston from 'winston';
import { config } from '../config';

export const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: () => (new Date().toLocaleTimeString()),
colorize: true,
level: config.application.mode === 'test' ? 'emerg' : 'info',
}),
new (require('winston-daily-rotate-file'))({
filename: `${process.cwd()}${config.application.log.dir}/.log`,
timestamp: () => (new Date().toLocaleTimeString()),
datePattern: 'yyyy-MM-dd',
prepend: true,
level: config.application.mode === 'test' ? 'emerg' : 'info',
maxsize: 100000000, // en octet
colorize: false,
json: false,
}),
],
});

export const generateLog = function (data: object): string {
let log = 'id="easy_tms" ';
for (const key in data) {
const value = (data[key] !== undefined && data[key] !== null) ? data[key] : '';
if (value instanceof Date) {
log += key + '="' + value.toISOString() + '" ';
} else if (Array.isArray(value) === true) {
log += key + '="' + value.join(',');
} else {
log += key + '="' + value.toString().replace(/"/g, '') + '" ';
}
}
return log;
};

编辑:

我测试了这个有效的解决方案,但我不确定我做得是否正确:

export function apiLogger(level: string, log: {
method?: any,
url?: any,
requestStep?: string,
responseStatus?: any,
responseMessage?: any,
response?: any,
}, endPoint: string): void {

const transportMain = new winston.transports.File({
timestamp: () => (new Date().toLocaleTimeString()),
datePattern: 'yyyy-MM-dd',
level: config.application.mode === 'test' ? 'emerg' : 'info',
filename: path.join(process.cwd(), config.application.log.dir, endPoint + '.log'),
maxsize: 1024 * 1024 * 100, // 100MB
colorize: false,
prepend: true,
json: false,
});

const loggerInstance = new (winston.Logger)({
transports: [
transportMain,
],
});

switch (level) {
case 'info':
loggerInstance.info(generateLog(log));
break;
case 'error':
loggerInstance.error(generateLog(log));
break;
case 'warn':
loggerInstance.warn(generateLog(log));
break;
case 'verbose':
loggerInstance.verbose(generateLog(log));
break;
case 'debug':
loggerInstance.debug(generateLog(log));
break;
case 'silly':
loggerInstance.silly(generateLog(log));
break;
default:
loggerInstance.info(generateLog(log));
break;
}
}

最佳答案

您可以为每个 API 创建一个记录器,每个记录器都有自己的文件:

const { Logger, transports } = require('winston');

const logger1 = new Logger({
transports: [new transports.File({ filename: 'log1.log' })]
});

const logger2 = new Logger({
transports: [new transports.File({ filename: 'log2.log' })]
});

logger1.info('log1');
logger2.info('log2');

Edit

您每次都在创建记录器,相反您可以执行以下操作:

const apis = ['api1', 'api2'];
const loggers = apis.reduce((memo, api) => {
memo[api] = new Logger({
transports: [new transports.File({ filename: `${api}.log` })]
});
return memo;
}, {});

const apiLogger = (level, message, api) => {
loggers[api].log(level, message);
};

apiLogger('info', 'msg1', 'api1');
apiLogger('info', 'msg2', 'api2');

关于javascript - winston 的多个日志文件具有最大大小和每日轮换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46732258/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com