我正在尝试使用 log4j2 syslog 附加程序将消息从我的 springboot 应用程序写入 syslog。
这是我的log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE XML>
<Configuration name="XMLConfig">
<Properties>
<Property name="LOG_PATTERN">%d %p %C{1.} [%t] %m%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n%throwable}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}" />
</Console >
<RollingFile name="RollingFile"
fileName="/data/storage/log/app.log"
filePattern="/data/storage/log/app.%d{yyyy-MM-dd}.%i.Logs.gz">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max ="100">
<Delete basePath="/data/storage/log/" maxDepth="2">
<IfFileName glob="*/app*.Logs.gz" />
<IfLastModified age="P7D" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<Syslog name="Syslog" format="RFC5424" host="127.0.0.1" port="514"
protocol="UDP" appName="myApp" enterpriseNumber="25979" facility="LOCAL0" />
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Root>
<Logger name="com.manjosh" level ="error" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="com.manjosh.cms.vnf.util.SyslogUtil" level ="debug" additivity="false">
<AppenderRef ref="Syslog"/>
</Logger>
</Loggers>
我也在我的服务器上启用了 rsyslog
[root@cmsnfv-c_7_1_53_0_standby_karthik ~]# sudo netstat -tulnp | grep 514
udp 0 0 127.0.0.1:514 0.0.0.0:* 2785/rsyslogd
我还看到 log4j 已正确初始化
2024-09-25 09:05:54,098 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a
2024-09-25 09:05:54,098 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a,component=StatusLogger
2024-09-25 09:05:54,098 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a,component=ContextSelector
2024-09-25 09:05:54,099 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a,component=Loggers,name=
2024-09-25 09:05:54,099 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a,component=Loggers,name=com.manjosh.cms.vnf.util.SyslogUtil
2024-09-25 09:05:54,099 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a,component=Loggers,name=com.manjosh
2024-09-25 09:05:54,099 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a,component=Appenders,name=Console
2024-09-25 09:05:54,099 main DEBUG Registering MBean org.apache.logging.log4j2:type=33c7353a,component=Appenders,name=RollingFile
2024-09-25 09:05:54,099 main DEBUG LoggerContext[name=33c7353a, org.apache.logging.log4j.core.LoggerContext@4524411f] started OK with configuration XmlConfiguration[location=/opt/dist/WebController/conf/log4j2.xml].
2024-09-25 09:05:54,279 main DEBUG AsyncLogger.ThreadNameStrategy=CACHED
但是,我仍然没有看到任何写入 /var/log/syslog 或 localO.log 的消息或消息 我看到控制台和文件附加器工作正常。
我使用的是springboot 1.5.4 我正在使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
你可以尝试跑步吗
sudo 服务 rsyslog 状态
确保没有错误。如果有任何错误,请重新启动服务。 希望 rsyslog 不会因为任何空间或内存问题而崩溃。