发布于2026-07-16 阅读(0)
扫一扫,手机访问
npm i winston
配置:
const winston = require('winston');
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
new winston.transports.File({ filename: 'logs/combined.log' })
]
});
运行时:LOG_LEVEL=debug node app.js
如果需要把应用日志和系统日志统一管理,可以对接 syslog。安装 npm i winston-syslog,然后配置一个 Syslog 传输端:
new winston.transports.Syslog({ host: 'localhost', facility: 'local0', tag: 'my-node-app' })
/etc/logrotate.d/nodejs 里配置一下:
/var/log/nodejs/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root adm
}
这样就能按天轮转,保留最近 7 天,旧的自动压缩,新文件权限也设置好了。
grep "ERROR" logs/combined.log | wc -l
查某个时间段的内容:
awk '/2025-12-04 10:00:00/,/2025-12-04 11:00:00/' logs/combined.log
如果日志量大了,还是得上集中化分析平台。ELK Stack 是比较经典的方案:Filebeat 或 Logstash 负责采集,Grok 做解析,Elasticsearch 存储,Kibana 做可视化(访问 http://your_ip:5601 就能看到)。Graylog 也是一个不错的选择,集中接收、索引、存储和分析海量日志。
系统日志统一查看可以用 journalctl:
journalctl -u rsyslog
journalctl -u my-node-app
配合 grep 和 awk 就能做过滤和统计。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8