在Ubuntu上监控Node.js日志可通过以下方式实现:
使用Node.js日志库
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'app.log' })
]
});
logger.info('This is an info log.');
系统工具实时查看
tail -f app.log
。journalctl -u node-app
。第三方监控工具
pm2 logs
),适合生产环境。其他方式
less
、grep
等命令分析。根据需求选择工具,轻量级场景可用Winston+tail,复杂场景推荐ELK或Grafana Loki。