用 Node.js 日志监控系统状态的可落地方案
一 核心架构与流程
二 落地步骤与关键代码示例
步骤1 结构化日志
使用 Winston 输出 JSON 日志,区分 error 与 combined 两类,便于后续检索与告警。
// logger.js
const winston = require('winston');
const { combine, timestamp, json } = winston.format;
const logger = winston.createLogger({
level: 'info',
format: combine(timestamp(), json()),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
new winston.transports.Console({ format: winston.format.simple() })
]
});
module.exports = logger;
在业务代码中打点:
const logger = require('./logger');
logger.info('user login', { userId: 'u123', ip: '1.2.3.4' });
logger.error('db connect failed', { err: err.message, retry: true });
步骤2 健康检查与系统状态
提供 /health 与 /status 端点,结合 os 模块输出关键运行信息,便于探针与快速排障。
// health.js
const express = require('express');
const os = require('os');
const logger = require('./logger');
const app = express();
app.get('/health', (req, res) => {
const health = { status: 'UP', uptime: process.uptime() };
logger.info('health check', health);
res.json(health);
});
app.get('/status', (req, res) => {
const mem = os.freemem() / os.totalmem();
const status = {
freeMemPct: (mem * 100).toFixed(2) + '%',
totalMem: (os.totalmem() / 1024 / 1024 / 1024).toFixed(2) + ' GB',
cpuCount: os.cpus().length,
systemUptime: os.uptime()
};
logger.info('status snapshot', status);
res.json(status);
});
app.listen(3000, () => logger.info('Server listening on 3000'));
步骤3 指标与可视化
使用 prom-client 暴露 /metrics,配合 Prometheus + Grafana 展示请求率、延迟、活跃请求等指标。
// metrics.js
const client = require('prom-client');
const httpRequestDuration = new client.Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'status']
});
const activeRequests = new client.Gauge({
name: 'active_requests',
help: 'Number of active HTTP requests'
});
function metricsMiddleware(req, res, next) {
const end = httpRequestDuration.startTimer();
activeRequests.inc();
res.on('finish', () => {
end({ method: req.method, status: res.statusCode });
activeRequests.dec();
});
next();
}
app.get('/metrics', async (req, res) => {
res.set('Content-Type', client.register.contentType);
res.end(await client.register.metrics());
});
步骤4 运行与日志管理
使用 PM2 启动与守护,并实时查看日志:
npm i -g pm2
pm2 start app.js --name my-app
pm2 logs my-app # 实时查看
pm2 monit # 资源监控
日志轮转(Linux 系统):
# /etc/logrotate.d/myapp
/path/to/logs/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 node node
}
三 告警与可视化配置
四 生产最佳实践