Ubuntu JS日志存储优化实操指南
一 核心策略与优先级
二 系统级治理与快速止血
sudo journalctl --disk-usagesudo nano /etc/systemd/journald.conf → 设置 SystemMaxUse=1G(按需调整)→ sudo systemctl restart systemd-journaldsudo journalctl --vacuum-time=7d;按服务清理:sudo journalctl --vacuum-time=7d -u service_name0 0 * * * find /path/to/logs -name "*.log" -mtime +30 -deletesudo apt cleandu -sh /path/to/logs/*、df -h三 应用内日志最佳实践
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new DailyRotateFile({
filename: '/var/log/myapp/application-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '100m',
maxFiles: '30d'
}),
new winston.transports.File({ filename: '/var/log/myapp/error.log', level: 'error' })
]
});
四 系统级日志轮转与保留策略
sudo nano /etc/logrotate.d/node-app/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
create 0640 myapp adm
copytruncate
}
sudo logrotate -d /etc/logrotate.d/node-app(调试),sudo logrotate -f /etc/logrotate.d/node-app(强制执行)。/etc/logrotate.d/rsyslog 中常见 size 100M、rotate 4、compress、weekly 的组合,可按需借鉴到应用日志。五 集中式日志与监控告警