把摩根和温斯顿套在一起会引起一些奇怪的角色



下面这篇文章:Node.js-logging/使用morgan和winston

我按如下方式设置记录器;

const winston = require('winston');
const datadogWinston = require('datadog-winston');
const os = require('os');
const logger = winston.createLogger({
level: 'info',
format: winston.format.simple(),
transports: [
new winston.transports.Console(),
new winston.transports.File({filename: 'test.log'}),
new datadogWinston({
apiKey: process.env.DATADOG_APIKEY,
hostname: os.hostname(),
service: 'korabo',
ddsource: 'nodejs',
}),
],
});
logger.stream = {
write: (message, _encoding) => {
logger.info(message);
},
};
module.exports = logger;

然而,每当winston记录一个项目时,它最终都是这样的;

info: [0mGET /stylesheets/style.css [36m304[0m 1.550 ms - -[0m

Morgan实例;

const logger = require('./lib/logger');
const app = express();
...
app.use(morgan('dev', {stream: logger.stream}));

如何修复日志中出现的那些奇怪字符?

您可以从消息中删除所有ANSI颜色/样式:从字符串中删除所有ANSI/颜色/样式

logger.stream = {
write: (message, _encoding) => {
logger.http(
message.replace(
/[u001bu009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
""
)
);
},
};

相关内容

  • 没有找到相关文章

最新更新