随笔 - 191, 文章 - 1, 评论 - 0, 阅读 - 33467
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux基础 - 服务管理 systemd服务的日志重定向

Posted on   Kingdomer  阅读(1115)  评论(0编辑  收藏  举报

 

问题:使用systemd管理open-falcon agent服务,agent日志输出到了/var/log/message下

     日志格式:Mar 3 06:00:19 localhost falcon-agent: 2022/03/03 06:00:19 plugin.go:64: <Plugins:[redis system], Timestamp:1677823219>

解决方法: 使用 systemd参数 与 rsyslog过滤器,配置syslog过滤器按程序名称拆分其输出。

 

一、systemd参数

实际操作中,systemd 参数未做调整

SyslogIdentifier:

  • 必须, 设置日志标识符(发送日志消息时加在行首的字符串)("syslog tag")。 默认值是进程的名称。
  • 此选项仅在 StandardOutput= 或 StandardError= 的值包含 journal(+console), syslog(+console), kmsg(+console) 之一时才有意义, 并且仅适用于输出到标准输出或标准错误的日志消息。


StandardOutput:

  • 必须, 设置进程的标准输出(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 过滤器设置

2.1 单一条件设置: 程序名称获取:在/var/log/messages日志中查看; 服务启动时的应用名称 ./bin/falcon-agent

1
2
3
[root@localhost ~]# ps -ef | grep agent
root     344916     1  0 08:08 ?        00:00:00 /application/open-falcon/agent/bin/falcon-agent -c /application/open-falcon/agent/config/cfg.json
root     258650 23872  0 08:41 pts/0    00:00:00 grep --color=auto agent

 

1
2
3
[root@localhost ~]# cat /etc/rsyslog.d/falcon-agent.conf
if $programname == 'falcon-agent' then /application/open-falcon/agent/logs/agent.log
if $programname == 'falcon-agent' then stop

2.2 多条件设置

1
2
3
4
5
6
7
8
9
10
if ($programname == 'progA') then {
   action(type="omfile" file="/var/log/progA.log")
   stop
} else if ($programname == 'progB') then {
   action(type="omfile" file="/var/log/progB.log")
   stop
} else if ($programname == 'progC') then {
   action(type="omfile" file="/var/log/progC.log")
   stop
}

2.3 检查语法是否正确

1
2
3
[root@localhost rsyslog.d]# rsyslogd -N1 -f falcon-agent.conf
rsyslogd: version 8.24.0-57.el7_9.3, config validation run (level 1), master config falcon-agent.conf
rsyslogd: End of config validation run. Bye.

三、 重启rsyslog, 启动服务

1
2
3
4
[root@localhost rsyslog.d]# systemctl restart rsyslog
[root@localhost rsyslog.d]# systemctl status rsyslog
 
[root@localhost ~]# systemctl restart falcon-agent

  

falcon-agent.service文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[Unit]
Description=OpenFalcon Agent Service
After=network.target
 
[Service]
Type=simple
WorkingDirectory=/application/open-falcon
#Environment=LOG_DIR=/application/open-falcon/agent/logs/agent.log
ExecStart=/application/open-falcon/agent/bin/falcon-agent -c /application/open-falcon/agent/config/cfg.json
#ExecStart=/usr/bin/cd /application/open-falcon && ./open-falcon start agent
#ExecStart=/application/open-falcon/open-falcon start agent
#ExecStop=/bin/bash -c "/application/open-falcon/open-falcon stop agent"
#ExecStop=/application/open-falcon/open-falcon stop agent
User=root
Group=root
Restart=on-failure
RestartSec=10s
StartLimitInterval=0
StartLimitBurst=10
LimitNOFILE=102400
LimitNPROC=102400
 
[Install]
WantedBy=multi-user.target

  

 

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示