gpt4 book ai didi

logging - winston 文件传输日志转义字符

转载 作者:行者123 更新时间:2023-12-02 05:16:45 28 4
gpt4 key购买 nike

我正在使用 winston 登录 Sails 应用程序。这是我的配置:

var customLogger = new winston.Logger({
transports: [
new(winston.transports.File)({
level: 'debug',
filename: 'app.log',
colorize: false,
showLevel: false,
prettyPrint: false,
exitOnError: false,
json: true,
zippedArchive: true,
maxsize: 1000000000,
maxFiles: 30,
tailable: true
}),
new(winston.transports.Console)({
level: 'info',
exitOnError: false,
colorize: false,
showLevel: false
})
],
});

但是在输出文件中有奇数字符。
{"level":"info","message":"\u001b[32minfo: \u001b[39m","timestamp":"2016-05-12T17:58:03.281Z"}

最佳答案

我确实遇到了类似的问题并通过删除 解决了它winston.format.colorize() 从下面的代码中删除奇怪或转义代码作为日志文件的一部分的语句。

const logFormat = winston.format.combine (
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
winston.format.colorize(),
winston.format.align(),
winston.format.printf(
info => `${info.timestamp} ${info.level.toUpperCase()}: ${info.message}`
));
下面共享的是完整的工作代码,没有任何与奇数或转义代码相关的问题。
作为 的一部分捕获的默认配置/zipCode/config/default.json 文件
{
"port": 3000,
"logConfig": {
"logFolder": ".//logs//",
"logFile": "zip-code-%DATE%.log",
"logLevel": "info"
}
}
将创建记录器实现作为 的一部分合并/zipCode/logger/log.js 文件
/**
* @author: dinesh.lomte
*/
const winston = require('winston');
const dailyRotateFile = require('winston-daily-rotate-file');
const config = require('config');
const logFormat = winston.format.combine (
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
winston.format.align(),
winston.format.printf(
info => `${info.timestamp} ${info.level.toUpperCase()}: ${info.message}`
)
);
const transport = new dailyRotateFile({
filename: config.get('logConfig.logFolder') + config.get('logConfig.logFile'),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
level: config.get('logConfig.logLevel')
});

transport.on('rotate', function(oldFile, newFile) {
// Call function like upload to s3 or on cloud
});

const logger = winston.createLogger({
format: logFormat,
transports: [
transport,
new winston.transports.Console({
level: config.get('logConfig.logLevel')
})
]
});

module.exports = logger;
以下 /zipCode/api/controller.js 显示记录器使用情况的文件
/**
* @author: dinesh.lomte
*/
'use strict';

var distance = require('../service/distance');
var logger = require('../logger/log');

var controllers = {
get_distance: function(req, res) {
logger.info('Processing get_distance request...');
distance.find(req, res, function(err, dist) {
if (err) {
logger.error('Failed to process distance request, due to: ' + err);
res.send(err);
}
logger.debug('Processed get_distance request...');
res.json(dist);
});
}
};

module.exports = controllers;

关于logging - winston 文件传输日志转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37194413/

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