我正在使用 winstonnode v16 express.js使用以下代碼示例在服務器上記錄傳入請求(歸結為重現行為的要領)。
我不確定是什么導致了這種行為差異,也找不到任何相關的檔案。
包裹:
"express": "^4.17.1",
"winston": "~3.3.3"
服務器腳本:
import express from 'express';
import winston from 'winston';
const app = express();
export const TEST_LOGGER = winston.createLogger(
{
level: 'silly',
format: winston.format.json(),
defaultMeta: { service: 'test' },
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp(),
winston.format.cli(),
),
}),
],
});
TEST_LOGGER.info(`\u27F5 Starting http logging`);
TEST_LOGGER.http(`\u27F5 Starting http logging`);
app.use((req, _response, next) =>
{
TEST_LOGGER.error(`\u27F5 ${ req.method } '${ req.originalUrl }'`);
TEST_LOGGER.warn(`\u27F5 ${ req.method } '${ req.originalUrl }'`);
TEST_LOGGER.info(`\u27F5 ${ req.method } '${ req.originalUrl }'`);
TEST_LOGGER.verbose(`\u27F5 ${ req.method } '${ req.originalUrl }'`);
TEST_LOGGER.http(`\u27F5 ${ req.method } '${ req.originalUrl }'`);
TEST_LOGGER.debug(`\u27F5 ${ req.method } '${ req.originalUrl }'`);
TEST_LOGGER.silly(`\u27F5 ${ req.method } '${ req.originalUrl }'`);
next();
});
app.listen(8080, () =>
{
TEST_LOGGER.info(`\u26A1 'server' launched`);
});
啟動服務器和呼叫時的日志/:
info: ? Starting http logging
http:undefined? Starting http logging
info: ? 'server' launched
error: ? GET '/'
warn: ? GET '/'
info: ? GET '/'
verbose: ? GET '/'
http:undefined? GET '/'
debug: ? GET '/'
silly: ? GET '/'
error: ? GET '/'
warn: ? GET '/'
info: ? GET '/'
verbose: ? GET '/'
http:undefined? GET '/'
debug: ? GET '/'
silly: ? GET '/'
error: ? GET '/'
warn: ? GET '/'
info: ? GET '/'
verbose: ? GET '/'
http:undefined? GET '/'
debug: ? GET '/'
silly: ? GET '/'
那些undefined來自哪里?
在 GitHub 上提出的問題:https ://github.com/winstonjs/logform/issues/183
uj5u.com熱心網友回復:
有趣的http是,源中的標準日志級別缺少日志級別:
https://github.com/winstonjs/winston/blob/master/lib/winston.js#L87
// Pass through the target methods onto `winston.
Object.keys(exports.config.npm.levels)
.concat([
'log',
'query',
'stream',
'add',
'remove',
'clear',
'profile',
'startTimer',
'handleExceptions',
'unhandleExceptions',
'handleRejections',
'unhandleRejections',
'configure',
'child'
])
.forEach(
method => (exports[method] = (...args) => defaultLogger[method](...args))
);
似乎來自包裝logform:
https://www.npmjs.com/package/logform
所以我會得出結論,這http不是一個有效的 winston 日志級別,這可能應該作為 winston github 上的一個問題提出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497075.html
