如何仅使用journalctl查看stderr日志?

如何仅使用journalctl查看stderr日志?

请假设有一个应用程序/脚本将大量日志数据打印到stdout.当脚本中发生预期的不良情况(异常处理)时,脚本会报告错误,stderr然后继续执行其正在执行的操作。

systemd当然能够收集这些数据并将其放入journal

# systemctl show sd_test.service | grep 'Standard[OE]'
StandardOutput=journal
StandardError=inherit

我的sd_test.service

[Unit]
Description=A Test Service simply printing to stdout and stderr

[Service]
Type=simple
ExecStart=/home/narunas/sd_test.py

[Install]
WantedBy=multi-user.target

journalctl现在有所需数据:

# systemctl reenable sd_test.service
# systemctl restart sd_test.service
# journalctl -n -u sd_test
-- Logs begin at Fri 2016-11-11 15:49:33 GMT, end at Tue 2017-01-03 19:23:18 GMT. --
Jan 03 19:23:01 dev-box sd_test.py[13183]: This is "stderr": 2
Jan 03 19:23:02 dev-box sd_test.py[13183]: This is "stdout": 2
Jan 03 19:23:03 dev-box sd_test.py[13183]: This is "stderr": 3
Jan 03 19:23:07 dev-box sd_test.py[13183]: This is "stdout": 3
Jan 03 19:23:07 dev-box sd_test.py[13183]: This is "stderr": 4
Jan 03 19:23:11 dev-box sd_test.py[13183]: This is "stdout": 4
Jan 03 19:23:12 dev-box sd_test.py[13183]: This is "stderr": 5
Jan 03 19:23:15 dev-box sd_test.py[13183]: This is "stdout": 5
Jan 03 19:23:17 dev-box sd_test.py[13183]: This is "stdout": 6
Jan 03 19:23:18 dev-box sd_test.py[13183]: This is "stderr": 6

我知道有_TRANSPORT日记字段,遗憾的stderr是标有相同的标签:

标准输出
对于那些从服务的标准输出或错误输出读取的内容

我当然可以改变我的ExecStart样子:

ExecStart=/bin/bash -c '/home/narunas/sd_test.py 2> /some/file/path'

但这并不理想,因为我正在journal与文件混淆......

stderr您能推荐一种仅查看日志的更好方法吗?

编辑_1

正如评论中所建议的,我刚刚尝试了-o verbose选项:

Tue 2017-01-03 20:23:49.994171 GMT [s=0b15e5c69e2f476eb200d2bdda769465;i=12e28;b=db55b41f61144ae69cc86acfb75209fb;m=42e034d52dc;t=5453672322abb;x=8b696c5447bc2bce]
    PRIORITY=6
    _UID=0
    _GID=0
    _CAP_EFFECTIVE=3fffffffff
    _SYSTEMD_SLICE=system.slice
    _BOOT_ID=db55b41f61144ae69cc86acfb75209fb
    _MACHINE_ID=c5a9e78e2c854065a9b041c58f07c2b2
    _HOSTNAME=dev-box
    SYSLOG_FACILITY=3
    _TRANSPORT=stdout
    SYSLOG_IDENTIFIER=sd_test.py
    _COMM=python3
    _EXE=/usr/bin/python3.5
    _CMDLINE=python3 /home/narunas/sd_test.py
    _SYSTEMD_CGROUP=/system.slice/sd_test.service
    _SYSTEMD_UNIT=sd_test.service
    MESSAGE=This is "stdout": 729
    _PID=13183
Tue 2017-01-03 20:23:52.979981 GMT [s=0b15e5c69e2f476eb200d2bdda769465;i=12e29;b=db55b41f61144ae69cc86acfb75209fb;m=42e037ae22e;t=54536725fba0d;x=8802c0df56848907]
    PRIORITY=6
    _UID=0
    _GID=0
    _CAP_EFFECTIVE=3fffffffff
    _SYSTEMD_SLICE=system.slice
    _BOOT_ID=db55b41f61144ae69cc86acfb75209fb
    _MACHINE_ID=c5a9e78e2c854065a9b041c58f07c2b2
    _HOSTNAME=dev-box
    SYSLOG_FACILITY=3
    _TRANSPORT=stdout
    SYSLOG_IDENTIFIER=sd_test.py
    _COMM=python3
    _EXE=/usr/bin/python3.5
    _CMDLINE=python3 /home/narunas/sd_test.py
    _SYSTEMD_CGROUP=/system.slice/sd_test.service
    _SYSTEMD_UNIT=sd_test.service
    MESSAGE=This is "stderr": 745
    _PID=13183

-o json-pretty产生与 相同的结果-o verbose,但格式不同。

除了我的剧本很愚蠢信息没有什么有用的东西可以让我们区分什么是stderrstdout

编辑_2

StandardError=journal

单元

[Unit]
Description=A Test Service simply printing to stdout and stderr

[Service]
Type=simple
ExecStart=/home/narunas/sd_test.py
StandardError=journal

[Install]
WantedBy=multi-user.target

日志控制

# systemctl reenable sd_test.service
# systemctl restart sd_test.service
# journalctl -n -u sd_test -o verbose
Tue 2017-01-03 22:34:55.381341 GMT [s=0b15e5c69e2f476eb200d2bdda769465;i=13ab7;b=db55b41f61144ae69cc86acfb75209fb;m=42fd81d9b7e;t=545384702735d;x=e65570c85dfec1dc]
    _TRANSPORT=stdout
    PRIORITY=6
    SYSLOG_FACILITY=3
    SYSLOG_IDENTIFIER=sd_test.py
    _UID=0
    _GID=0
    _COMM=python3
    _EXE=/usr/bin/python3.5
    _CMDLINE=python3 /home/narunas/sd_test.py
    _CAP_EFFECTIVE=3fffffffff
    _SYSTEMD_CGROUP=/system.slice/sd_test.service
    _SYSTEMD_UNIT=sd_test.service
    _SYSTEMD_SLICE=system.slice
    _BOOT_ID=db55b41f61144ae69cc86acfb75209fb
    _MACHINE_ID=c5a9e78e2c854065a9b041c58f07c2b2
    _HOSTNAME=dev-box
    MESSAGE=This is "stdout": 10
    _PID=21280
Tue 2017-01-03 22:34:57.397880 GMT [s=0b15e5c69e2f476eb200d2bdda769465;i=13ab8;b=db55b41f61144ae69cc86acfb75209fb;m=42fd83c6099;t=5453847213878;x=610237e701d596d4]
    _TRANSPORT=stdout
    PRIORITY=6
    SYSLOG_FACILITY=3
    SYSLOG_IDENTIFIER=sd_test.py
    _UID=0
    _GID=0
    _COMM=python3
    _EXE=/usr/bin/python3.5
    _CMDLINE=python3 /home/narunas/sd_test.py
    _CAP_EFFECTIVE=3fffffffff
    _SYSTEMD_CGROUP=/system.slice/sd_test.service
    _SYSTEMD_UNIT=sd_test.service
    _SYSTEMD_SLICE=system.slice
    _BOOT_ID=db55b41f61144ae69cc86acfb75209fb
    _MACHINE_ID=c5a9e78e2c854065a9b041c58f07c2b2
    _HOSTNAME=dev-box
    MESSAGE=This is "stderr": 11
    _PID=21280
Tue 2017-01-03 22:35:02.403014 GMT [s=0b15e5c69e2f476eb200d2bdda769465;i=13abc;b=db55b41f61144ae69cc86acfb75209fb;m=42fd888bfe7;t=54538476d97c6;x=a6362820cb52ce9]
    _TRANSPORT=stdout
    PRIORITY=6
    SYSLOG_FACILITY=3
    SYSLOG_IDENTIFIER=sd_test.py
    _UID=0
    _GID=0
    _COMM=python3
    _EXE=/usr/bin/python3.5
    _CMDLINE=python3 /home/narunas/sd_test.py
    _CAP_EFFECTIVE=3fffffffff
    _SYSTEMD_CGROUP=/system.slice/sd_test.service
    _SYSTEMD_UNIT=sd_test.service
    _SYSTEMD_SLICE=system.slice
    _BOOT_ID=db55b41f61144ae69cc86acfb75209fb
    _MACHINE_ID=c5a9e78e2c854065a9b041c58f07c2b2
    _HOSTNAME=dev-box
    MESSAGE=This is "stderr": 12
    _PID=21280

答案1

A拉取请求实现该功能目前正在审核中。

同时,如果您控制本机运行的脚本,您可以使用python-systemd模块将消息从脚本发送到日志,并包含您可能希望的优先级和选项。

答案2

如果您在消息前面加上 <优先级> 您的消息将按照该优先级记录在日志中。您可以找到包含级别、简要说明和一些使用示例的列表在这个链接

在我的机器上编译这段代码

#include <cstdio>
#include <thread>
#include <chrono>

const char* pri_msg[] {
    "EMERGENCY",
    "ALERT",
    "CRITICAL",
    "ERROR",
    "WARNING",
    "NOTICE",
    "INFO",
    "DEBUG"
};

int main(){
    int pri = 0;
    while(true) {
        std::printf("<%d>Test service priority level: %s", pri, pri_msg[pri]);
        pri = ++pri % 8;
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }
}

并将其作为 systemd 服务运行,每条消息都以其优先级记录到日志中。

然后,您可以使用以下命令检索它们journalctl -u *your-service* -p *priority-level*

$ journalctl -u testd -p 3
-- Logs begin at mer 2020-10-21 17:31:13 CEST, end at ven 2020-10-23 16:01:01 CEST. --
ott 23 15:57:30 legion testd[21967]: Test service priority level: EMERGENCY
ott 23 15:57:35 legion testd[21967]: Test service priority level: ALERT
ott 23 15:57:40 legion testd[21967]: Test service priority level: CRITICAL
ott 23 15:57:45 legion testd[21967]: Test service priority level: ERROR
ott 23 15:58:10 legion testd[21967]: Test service priority level: EMERGENCY
ott 23 15:58:15 legion testd[21967]: Test service priority level: ALERT
ott 23 15:58:20 legion testd[21967]: Test service priority level: CRITICAL
ott 23 15:58:25 legion testd[21967]: Test service priority level: ERROR
ott 23 15:58:50 legion testd[21967]: Test service priority level: EMERGENCY
ott 23 15:58:55 legion testd[21967]: Test service priority level: ALERT
ott 23 15:59:00 legion testd[21967]: Test service priority level: CRITICAL

在您的情况下,我只需在用<3>std::cerrfprintf(stderr, ...).

相关内容