AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 573946
Accepted
mikemaccana
mikemaccana
Asked: 2014-02-08 05:23:06 +0800 CST2014-02-08 05:23:06 +0800 CST 2014-02-08 05:23:06 +0800 CST

如何从命令行向 systemd 日志发送消息?

  • 772

在较旧的 Linux 系统中,该logger命令可用于向 syslog 发送日志消息。

阅读Arch Linux 中将其消息记录到哪里?logger,如果设置了用于消息转发的套接字syslog,消息和logger命令行应用程序似乎只与systemd日志对话。

那么logger命令的现代等价物是什么?如何从命令行直接向 systemd 日志发送消息?

logging
  • 2 2 个回答
  • 56390 Views

2 个回答

  • Voted
  1. Best Answer
    mikemaccana
    2014-02-08T05:39:37+08:002014-02-08T05:39:37+08:00

    systemd-cat相当于记录器:

    echo 'hello' | systemd-cat
    

    在另一个终端中,运行journalctl -f:

    Feb 07 13:38:33 localhost.localdomain cat[15162]: hello
    

    优先级仅由字符串的一部分指定:

    echo 'hello' | systemd-cat -p info
    echo 'hello' | systemd-cat -p warning
    echo 'hello' | systemd-cat -p emerg
    

    警告是粗体的,紧急情况是粗体和红色的。可怕的东西。

    您还可以使用任意的“标识符”来指定应用程序名称。这些就像 syslog 的旧设施,但您不会被古老的东西所束缚,lpr uucp nntp例如.local0local7

    echo 'hello' | systemd-cat -t someapp -p emerg
    

    记录为:

    Feb 07 13:48:56 localhost.localdomain someapp[15278]: hello
    
    • 110
  2. kkm
    2020-10-30T17:00:04+08:002020-10-30T17:00:04+08:00

    由于问题提到了 Arch Linux(从第一天开始由 systemd 控制)和日志记录,我会冒险猜测它与来自 systemd 服务的日志记录有关。这是从 systemd 服务单元调用的shell 脚本的另一种日志记录技术。systemd 可以(并且默认情况下)设置为侦听服务的进程 stderr 和/或 stdout,并将消息转发到日志。当消息以 3 个字符的前缀开头时,其中N是从 0 到 7 的数字,systemd 将其解释为日志级别,忽略它,并在指定级别记录字符串的其余部分。'<' N '>'

    这很方便,因为来自任何命令的任何 stderr 消息都将自动记录在错误级别。另一个 fd 保留用于以任意不同的严重性进行日志记录。

    这当然适用于所有程序,而不仅仅是 shell 脚本。

    例子

    取自真实服务脚本的shell 脚本(在本例中为 Bash,依赖于进程替换和行为) :trap ... EXITExecStartPre

    #!/bin/bash
    
    # Redirect stderr such that any message is reported as an error to journald by
    # prepending '<3>' to it. Use fd 4 (the saved stderr) to directly report other
    # severity levels.
    exec 4>&2 2> >(while read -r REPLY; do printf >&4 '<3>%s\n' "$REPLY"; done)
    
    # Systemd can kill the logging subshell swiftly when we exit, and lose messages.
    # Close the subshell before exiting the main program explicitly. This will block
    # until the read builtin reads the EOF.
    trap 'exec >&2-' EXIT
    
    ### From this point on, all stderr messages will be logged to syslog as errors
    ### via the subshell running the while loop. Fd 4 is the old stderr, and may
    ### be used to specify non-error level:
    
    echo >&2 "This message is logged as an error, red and bold."
    
    echo >&4 "<5>This is logged as a notice-level message, dim and boring."
    
    echo >&4 "This is logged at the default severity level, 'info' unless changed."
    

    单元文件中的设置一般不需要特别设置。stderr logging 开箱即用,除非在systemd-system.conf (5) 或journald.conf (5) 中全局覆盖。默认值为:

    [Service]
    ; These defaults may be overridden in systemd-system.conf(5).
    ;StandardError=journal
    ;StandardOutput=journal
    
    ; These defaults may be overridden in journald.conf(5).
    ;LogLevelMax=debug
    ;LogRateLimitIntervalSec=10000
    ;LogRateLimitBurst=30s
    
    ; Extra fields may be added to every message.
    ;LogExtraFields=
    
    ;; Other settings:
    
    ; Defaults to process name. NOT the unit name, but rather basename(3) of the $0.
    ;SyslogIdentifier=
    
    ; For messages written without the <N> prefix.
    ;SyslogLevel=info
    
    ;SyslogFacility=daemon
    
    ; For completeness only: The level-parsing machinery can be disabled, but that
    ; was the whole point...
    ;SyslogLevelPrefix=true
    

    请注意,systemd 重定向所有使用设置调用的命令Exec*,不仅是主服务进程ExecStart,还有ExecStartPre,ExecStartPost等。

    要运行该示例,请将上述脚本另存为logging-test.sh,使用 systemd-run 作为临时单元运行,然后查询每个日志记录的完整属性。如果您没有看到信息级别消息,请检查 journald.conf 是否将日志中存储的日志级别限制为更高的值。

    $ systemd-run --user --wait ./logging-test.sh
    $ journalctl -t logging-test.sh
    $ journalctl -t logging-test.sh -o json-pretty
    

    日志级别在sd-daemon (3)中定义:

    #define SD_EMERG   "<0>"  /* system is unusable */
    #define SD_ALERT   "<1>"  /* action must be taken immediately */
    #define SD_CRIT    "<2>"  /* critical conditions */
    #define SD_ERR     "<3>"  /* error conditions */
    #define SD_WARNING "<4>"  /* warning conditions */
    #define SD_NOTICE  "<5>"  /* normal but significant condition */
    #define SD_INFO    "<6>"  /* informational */
    #define SD_DEBUG   "<7>"  /* debug-level messages */
    

    参考

    • systemd.exec (5),日志和标准输入/输出。
    • sd 守护进程(3)。
    • systemd-system.conf (5) (文件名通常systemd.conf为 ,此处命名为手册页)。
    • journald.conf (5)。
    • 7

相关问题

  • IIS 6 - 仅记录某些目录

  • 什么是好的日志查看器,例如 apache、postfix、syslog?

  • 如何提供可搜索的 IRC 日志?

  • 避免将某些丢失的文件记录到 Apache2 错误日志中

  • Tomcat 6 HTTP 日志滚动和清除

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve