diff --git a/lib/config.js b/lib/config.js index 4e76cf06d8..d435c4e50e 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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, diff --git a/lib/utils/logger.js b/lib/utils/logger.js index 6b9136ccc3..c92e4f13df 100644 --- a/lib/utils/logger.js +++ b/lib/utils/logger.js @@ -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, }); @@ -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', }) ); diff --git a/website/docs/install/README.md b/website/docs/install/README.md index ea25ad0e7e..d45c831d23 100644 --- a/website/docs/install/README.md +++ b/website/docs/install/README.md @@ -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` diff --git a/website/i18n/zh/docusaurus-plugin-content-docs/current/install/README.md b/website/i18n/zh/docusaurus-plugin-content-docs/current/install/README.md index 6244cdb0af..fb04235912 100644 --- a/website/i18n/zh/docusaurus-plugin-content-docs/current/install/README.md +++ b/website/i18n/zh/docusaurus-plugin-content-docs/current/install/README.md @@ -672,6 +672,8 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行 `NO_LOGFILES`: 是否禁用日志文件输出,默认 `false` +`SHOW_LOGGER_TIMESTAMP`: 在控制台输出中显示日志时间戳,默认 `false` + `SENTRY`: [Sentry](https://sentry.io) dsn,用于错误追踪 `SENTRY_ROUTE_TIMEOUT`: 路由耗时超过此毫秒值上报 Sentry,默认 `3000`