在Ubuntu系统中,监控Node.js应用程序的错误日志可以通过以下几种方法实现:
console.error()或console.log()输出错误信息: 在你的Node.js应用程序中,使用console.error()或console.log()将错误信息输出到控制台。这样,当应用程序出现错误时,你可以在运行应用程序的终端中看到错误信息。try { // Your code here } catch (error) { console.error('Error:', error); } 例如,使用Winston库:
const winston = require('winston'); const logger = winston.createLogger({ level: 'error', format: winston.format.json(), transports: [ new winston.transports.File({ filename: 'error.log' }), ], }); try { // Your code here } catch (error) { logger.error('Error:', error); } 例如,使用PM2:
# 安装PM2 npm install pm2 -g # 使用PM2启动应用程序 pm2 start app.js --name my-app # 查看应用程序日志 pm2 logs my-app 通过以上方法,你可以有效地监控Ubuntu系统中Node.js应用程序的错误日志,并及时发现和解决问题。