在Debian系统上监控Node.js应用程序的日志有多种方法,以下是一些常用的方法:
sudo apt update
sudo apt install nodejs npm
sudo npm install winston
const winston = require('winston');
// 创建一个winston日志记录器
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: '/var/log/nodejs_system.log', level: 'info' })
]
});
// 记录不同级别的日志
logger.info('This is an informational message');
logger.error('This is an error message');
nohup node /path/to/your_script.js &
sudo journalctl -b # 查看最近的系统启动日志
sudo journalctl -u 服务名称 # 查看某个特定服务的日志
sudo journalctl --since "2021-01-01" --until "2021-01-31" # 查看某个特定时间范围内的日志
sudo tail -f /var/log/syslog # 实时查看系统日志的最后几行
通过上述方法,你可以在Debian系统上有效地监控和管理Node.js应用程序的日志。