• #">systemd所需参数为#
  • #">rsyslog过滤器设置#

    有一种更优雅的方法可以解决systemd输出到指定文件而非/var/log/message,需要使用systemd参数与rsyslog过滤器。并指示syslog过滤器按程序名称拆分其输出。

    systemd所需参数为#

    • SyslogIdentifierrequired,设置日志标识符(发送日志消息时加在行首的字符串)(“syslog tag”)。 默认值是进程的名称。此选项仅在 StandardOutput= 或 StandardError= 的值包含 journal(+console), syslog(+console), kmsg(+console) 之一时才有意义, 并且仅适用于输出到标准输出或标准错误的日志消息。
    • StandardOutputrequired,设置进程的标准输出(STDOUT)。 可设为 inherit, null, tty, journal, syslog, kmsg, journal+console, syslog+console, kmsg+console, file:path, append:path, socket, fd:name 之一。
    • StandardError:设置进程的标准错误(STDERR)。 取值范围及含义与 StandardOutput= 相同。但有如下例外: (1) inherit 表示使用 StandardOutput= 的值。 (2) fd:name 的默认文件描述符名称为 “stderr”

      rsyslog过滤器设置#

      使用rsyslog条件选择器。如果不改变rsyslog目前工作模式,按照如下操作:
    1. 新建/etc/rsyslog.d/xx.conf文件
    2. 在新建文件内写入内容如下

    单一条件处理。

    1. if $programname == 'programname' then /var/log/programname.log
    2. # 停止往其他文件内写入,如果不加此句,会继续往/var/log/message写入。
    3. if $programname == 'programname' then stop

    多条件处理
    会根据不同应用名称将不同的输出日志重定向到不同的文件内。

    1. if ($programname == 'apiserver') then {
    2. action(type="omfile" file="/var/log/apiserver.log")
    3. stop
    4. } else if ($programname == 'scheduler') then {
    5. action(type="omfile" file="/var/log/scheduler.log")
    6. stop
    7. } else if ($programname == 'controller-manager') then {
    8. action(type="omfile" file="/var/log/controller-manager.log")
    9. stop
    10. } else if ($programname == 'etcd') then {
    11. action(type="omfile" file="/var/log/etcd.log")
    12. stop
    13. }
    1. 检查语法是否正确 rsyslogd -N1 -f file_name.conf
    2. 重新启动rsyslog

    完成以上步骤后,应用的 stdout stderr被重定向到对应的日志文件内了,而非/var/log/message,并且仍然可以通过通过journalctl获得对应的stdout stderr (systemd参数机制)。