首页主机资讯如何监控Ubuntu JS日志

如何监控Ubuntu JS日志

时间2025-10-01 09:39:03发布访客分类主机资讯浏览1230
导读:监控Ubuntu上JavaScript(Node.js)日志的方法 一、基础实时监控工具 1. 使用tail命令实时查看日志 tail -f是Linux系统最常用的实时日志查看工具,可直接跟踪日志文件的新增内容。例如,若应用日志保存在/va...

监控Ubuntu上JavaScript(Node.js)日志的方法

一、基础实时监控工具

1. 使用tail命令实时查看日志

tail -f是Linux系统最常用的实时日志查看工具,可直接跟踪日志文件的新增内容。例如,若应用日志保存在/var/log/my-js-app.log,执行以下命令即可实时查看:
tail -f /var/log/my-js-app.log
Ctrl+C可停止监控。此方法适合快速查看实时日志流,无需额外配置。

2. 使用journalctl查看系统日志

若JavaScript应用通过systemd以服务形式运行(如node-app.service),可使用journalctl查看其日志。常用命令:

  • 查看所有日志:journalctl
  • 查看特定服务日志:journalctl -u your-js-service-name
  • 实时查看服务日志:journalctl -u your-js-service-name -f
  • 筛选错误日志:journalctl -p err -u your-js-service-name
    此方法适合集中管理系统日志,无需直接访问应用日志文件。

二、应用层日志记录配置

1. 使用Winston日志库(推荐)

Winston是Node.js生态中最流行的日志库,支持多传输(文件、控制台、数据库等)、多级别(info/warn/error)、结构化日志。安装与配置示例:

npm install winston

配置文件(如logger.js):

const winston = require('winston');

const logger = winston.createLogger({

  level: 'info', // 日志级别(info及以上会记录)
  format: winston.format.json(), // 结构化日志(便于后续分析)
  transports: [
    new winston.transports.Console(), // 输出到控制台
    new winston.transports.File({
 filename: 'logs/error.log', level: 'error' }
), // 错误日志单独保存
    new winston.transports.File({
 filename: 'logs/combined.log' }
) // 所有日志合并
  ]
}
    );
    
module.exports = logger;
    

在应用中使用:

const logger = require('./logger');
    
logger.info('Server started on port 3000');
    
logger.error('Database connection failed');

Winston的优势在于灵活的日志分级结构化输出,便于后续通过ELK等工具分析。

2. 使用PM2进程管理器

PM2是Node.js生产环境的常用进程管理工具,内置日志管理、进程守护、负载均衡功能。安装与使用:

npm install pm2 -g

启动应用并记录日志:

pm2 start app.js --name "my-js-app" --log /var/log/my-js-app.log

查看实时日志:

pm2 logs my-js-app

日志轮转配置(避免日志过大):

pm2 set pm2-logrotate:max_size 10M # 单个日志文件最大10MB
pm2 set pm2-logrotate:retain 7    # 保留最近7天日志

PM2适合生产环境,简化了日志管理与进程守护流程。

三、集中式日志管理方案

1. ELK Stack(Elasticsearch+Logstash+Kibana)

ELK是开源的日志集中化管理与分析平台,适合大规模应用。搭建步骤:

  • 安装Elasticsearch
    sudo apt-get install elasticsearch
    sudo systemctl start elasticsearch
    
  • 安装Logstash
    sudo apt-get install logstash
    
    配置Logstash收集Node.js日志(/etc/logstash/conf.d/nodejs.conf):
    input {
    
      file {
        
        path =>
         "/var/log/my-js-app.log"
        start_position =>
     "beginning"
      }
    
    }
    
    output {
    
      elasticsearch {
        
        hosts =>
         ["localhost:9200"]
        index =>
     "nodejs-logs-%{
    +YYYY.MM.dd}
    "
      }
    
    }
        
    
  • 安装Kibana
    sudo apt-get install kibana
    sudo systemctl start kibana
    
    访问http://your-server-ip:5601,通过Kibana创建仪表盘,可视化日志数据(如错误率、请求频率)。

2. Prometheus+Grafana(监控与可视化)

若需监控应用性能指标(如请求延迟、内存使用),可结合Prometheus(指标收集)与Grafana(可视化)。步骤:

  • 安装Prometheus客户端库
    npm install prom-client
    
    在应用中定义指标(如http_requests_total):
    const promClient = require('prom-client');
    
    const httpRequestCounter = new promClient.Counter({
    
      name: 'http_requests_total',
      help: 'Total HTTP requests',
      labelNames: ['method', 'path']
    }
        );
        
    app.use((req, res, next) =>
     {
    
      httpRequestCounter.inc({
     method: req.method, path: req.path }
        );
        
      next();
    
    }
        );
    
    
  • 配置Prometheus
    编辑/etc/prometheus/prometheus.yml,添加Node.js应用作为目标:
    scrape_configs:
      - job_name: 'nodejs'
        static_configs:
          - targets: ['localhost:9090'] # 应用需暴露Prometheus metrics接口
    
  • 配置Grafana
    添加Prometheus为数据源,导入Node.js监控仪表盘(如ID:1860),实时查看性能指标。

四、日志轮转管理

为避免日志文件过大占用磁盘空间,需配置日志轮转。使用logrotate工具(Ubuntu自带):

  • 创建配置文件(/etc/logrotate.d/my-js-app):
    /var/log/my-js-app.log {
    
      daily       # 每天轮转
      missingok   # 文件不存在时不报错
      rotate 7    # 保留最近7份
      compress    # 压缩旧日志(.gz格式)
      notifempty  # 空日志不轮转
      create 640 root adm # 新日志权限
    }
        
    
  • 测试配置:
    sudo logrotate -f /etc/logrotate.d/my-js-app
    

此方法可自动化日志管理,减少人工干预。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 如何监控Ubuntu JS日志
本文地址: https://pptw.com/jishu/715543.html
怎样通过dmesg诊断系统启动问题 Linux dmesg中的驱动程序加载顺序是什么

游客 回复需填写必要信息