如何将节点红色日志存储在本地存储中?

How to store node-red logs in local storage?

我在 Raspbian 中设置了 node-red,我想将来自 node-red 客户端的日志存储在某个存储位置,例如 .log 文件。

有两种方法可以做到这一点。

  1. 为标准日志记录模块编写一个日志函数。 https://nodered.org/docs/user-guide/logging
  2. 使用第三方 node-red 模块来记录到文件。像 - https://flows.nodered.org/node/node-red-contrib-advance-logger

Node-RED 在 raspbian 上的默认安装会将其设置为一项服务,并且日志将已发送至系统日志。

可以使用 node-red-log 工具或使用 journalctrl 命令访问日志

在 linux 分发系统中存储 node-red 日志的 hack 只需按照以下步骤操作:-

  1. /etc/systemd/system/ 中创建自定义 node-red 服务 制作 .service 文件的命令 nano /etc/systemd/system/node-red-custom.service

    [Unit]
    Description=Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.
    After=syslog.target network.target
    Documentation=http://nodered.org/
    
    [Service]
    #Full Path to Node.js
    ExecStart=  /usr/bin/node-red
    WorkingDirectory=/root/node-red/
    # User/Group that launches node-RED (it's advised to create a new user for Node-RED)
    # You can do : sudo useradd node-red
    # then change the User=root by User=node-red
    User=root
    Group=root
    Nice=10
    #SyslogIdentifier=Node-RED
    SyslogIdentifier=node-red-custom
    StandardOutput=syslog
    StandardError=syslog
    # Make Node-RED restart if it fails
    Restart=on-failure
    # Node-RED need a SIGINT to be notified to stop
    KillSignal=SIGINT
    
    [Install]
    WantedBy=multi-user.target
    

    2.Make配置文件,目标是你想在哪里存储日志

    nano /etc/rsyslog.d/node-red-custom.conf

    if $programname == 'node-red-custom' then /var/log/node-red-logs.log
    & stop
    

创建这两个文件后,请运行执行以下命令

sudo systemctl restart rsyslog
sudo systemctl enable node-red-custom.service
sudo systemctl start node-red-custom.service

现在您的自定义 node-red 服务开始将日志存储在 /var/log/node-red-logs.log

注意:- 您必须先终止 运行ning node-red 服务,然后才能如上所述启用自定义 node-red 服务。