在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應用程序的錯誤日志,并及時發現和解決問題。