Suponha que exista um aplicativo / script que imprima uma grande quantidade de dados de registro em log para stdout
. Quando algo ruim, mas esperado, acontece no script (manipulação de exceção), os scripts informam um erro para stderr
e, em seguida, continuam fazendo o que estava fazendo.
systemd
com certeza é capaz de coletar esses dados e colocá-los no journal
:
# systemctl show sd_test.service | grep 'Standard[OE]'
StandardOutput=journal
StandardError=inherit
Meu 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
agora exigiu dados:
# 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
Eu sei que há _TRANSPORT
campo de diário, infelizmente stderr
está marcado com a mesma tag:
stdout
for those read from a service's standard output or error output
Claro que posso alterar meu ExecStart
da seguinte forma:
ExecStart=/bin/bash -c '/home/narunas/sd_test.py 2> /some/file/path'
Mas isso não é ideal, pois estou mixando journal
com arquivos ...
Você poderia recomendar uma maneira melhor de visualizar somente stderr
log?
EDIT_1
Como sugerido nos comentários, tentei apenas -o verbose
option:
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
produz os mesmos resultados que -o verbose
, mas em um formato diferente.
Além do bobo MESSAGE do meu script, não há nada de útil que permita distinguir o que é stderr
ou stdout
.
EDIT_2
com StandardError=journal
.
Unidade :
[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
journalctl :
# 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