Skip to content

Commit

Permalink
feat(core): add logger timestamp (#13165)
Browse files Browse the repository at this point in the history
* feat(core): add logger timestamp

* Update website/docs/install/README.md

---------
  • Loading branch information
JimenezLi authored Aug 30, 2023
1 parent 64839f0 commit 121763b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const calculateValue = () => {
debugInfo: envs.DEBUG_INFO || 'true',
loggerLevel: envs.LOGGER_LEVEL || 'info',
noLogfiles: envs.NO_LOGFILES,
showLoggerTimestamp: envs.SHOW_LOGGER_TIMESTAMP,
sentry: {
dsn: envs.SENTRY,
routeTimeout: parseInt(envs.SENTRY_ROUTE_TIMEOUT) || 30000,
Expand Down
16 changes: 14 additions & 2 deletions lib/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ if (!config.noLogfiles) {
}
const logger = winston.createLogger({
level: config.loggerLevel,
format: winston.format.json(),
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
winston.format.printf((info) =>
JSON.stringify({
timestamp: info.timestamp,
level: info.level,
message: info.message,
})
)
),
transports,
});

Expand All @@ -25,7 +34,10 @@ const logger = winston.createLogger({
if (!config.isPackage) {
logger.add(
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
format: winston.format.printf((info) => {
const infoLevel = winston.format.colorize().colorize(info.level, config.showLoggerTimestamp ? `[${info.timestamp}] ${info.level}` : info.level);
return `${infoLevel}: ${info.message}`;
}),
silent: process.env.NODE_ENV === 'test',
})
);
Expand Down
2 changes: 2 additions & 0 deletions website/docs/install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ See the relation between access key/code and white/blacklisting.
`NO_LOGFILES`: disable logging to log files, default to `false`
`SHOW_LOGGER_TIMESTAMP`: Show timestamp in log, default to `false`
`SENTRY`: [Sentry](https://sentry.io) dsn, used for error tracking
`SENTRY_ROUTE_TIMEOUT`: Report Sentry if route execution takes more than this milliseconds, default to `3000`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行
`NO_LOGFILES`: 是否禁用日志文件输出,默认 `false`
`SHOW_LOGGER_TIMESTAMP`: 在控制台输出中显示日志时间戳,默认 `false`
`SENTRY`: [Sentry](https://sentry.io) dsn,用于错误追踪
`SENTRY_ROUTE_TIMEOUT`: 路由耗时超过此毫秒值上报 Sentry,默认 `3000`
Expand Down

0 comments on commit 121763b

Please sign in to comment.