在 Debian 系统上通过 Node.js 日志排查故障,可以按照以下步骤进行:
journalctl 命令查看系统日志: journalctljournalctl -bjournalctl -fjournalctl -u 服务名 或 journalctl -e "事件描述"。logrotate 工具进行日志文件轮换,防止日志文件过大。。tail -f 命令实时查看日志文件的最后几行,这对于监控实时日志非常有用。。安装 Winston:
npm install winston 配置 Winston:
const winston = require('winston'); const logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ] }); if (process.env.NODE_ENV !== 'production') { logger.add(new winston.transports.Console({ format: winston.format.simple() })); } 在应用程序中使用 Winston 记录日志:
logger.info('Hello World!'); logger.error('An error occurred'); ```。 通过以上步骤,你可以有效地查看和分析 Debian 系统中 Node.js 应用的日志,从而进行故障排查。记得在处理日志时注意敏感信息,避免泄露。。