log4j22.properties syslog并不是wringing

发布于 2025-01-22 01:42:07 字数 1635 浏览 0 评论 0 原文

我的Java应用程序问题是log4j2 syslog不是在“ local1.log”中写的,而是 “消息”。 my/etc/rsyslog.conf在/etc/rsyslog.conf中配置了'local1。*/var/log/local1.log'。

但是,当我从log4j22.properties中删除'appender.syslog.layout.type'''appender.syslog.layout.type'和'appender.syslog.pattern'时,syslog开始写在/var/log/local1.log上时。

我的配置不正确吗?

Syslog中是否不应用布局属性?

[/etc/rsyslog.conf]

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none;local1.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

...

local1.*                                                /var/log/local1.log

[使用的log4j2库]

log4j-api-2.17.2.jar
log4j-core-2.17.2.jar

[log4j2.properties]

status = warn
name = Test

# Console appender configuration
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss} %5p (%c{1} - %M:%L) - %m%n

appender.syslog.type = Syslog
appender.syslog.name = sysLogger
appender.syslog.host = localhost
appender.syslog.port = 514
appender.syslog.protocol = UDP
appender.syslog.facility = LOCAL1
appender.syslog.layout.type = PatternLayout
appender.syslog.layout.pattern = %c{1} (%M:%L) %m\n

# Root logger level
rootLogger.level = debug
rootLogger.appenderRefs = consoleLogger, sysLogger
rootLogger.appenderRef.stdout.ref = consoleLogger
rootLogger.appenderRef.syslog.ref = sysLogger

My java application problem is that log4j2 syslog is written not in 'local1.log' but
'messages'.
My /etc/rsyslog.conf is configured 'local1.* /var/log/local1.log' in /etc/rsyslog.conf.

But One of weired is when I removed 'appender.syslog.layout.type' and 'appender.syslog.layout.pattern' from log4j2.properties, syslog starts being written in /var/log/local1.log correctly.

Is my configuration incorrect?

Are layout properties not applied in syslog?

[/etc/rsyslog.conf]

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none;local1.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

...

local1.*                                                /var/log/local1.log

[Used log4j2 library]

log4j-api-2.17.2.jar
log4j-core-2.17.2.jar

[log4j2.properties]

status = warn
name = Test

# Console appender configuration
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{HH:mm:ss} %5p (%c{1} - %M:%L) - %m%n

appender.syslog.type = Syslog
appender.syslog.name = sysLogger
appender.syslog.host = localhost
appender.syslog.port = 514
appender.syslog.protocol = UDP
appender.syslog.facility = LOCAL1
appender.syslog.layout.type = PatternLayout
appender.syslog.layout.pattern = %c{1} (%M:%L) %m\n

# Root logger level
rootLogger.level = debug
rootLogger.appenderRefs = consoleLogger, sysLogger
rootLogger.appenderRef.stdout.ref = consoleLogger
rootLogger.appenderRef.syslog.ref = sysLogger

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和影子一齐双人舞 2025-01-29 01:42:07

Log4J2的Syslog布局用于格式化 syslog消息,因此必须是 sysloglayout (传统的BSD syslog格式)或 rfc5424layOut (现代系统锁定布局)。使用任何其他布局将导致无效的消息,RSYSLOG必须猜测消息的元数据。最值得注意的是,该设施将设置为用户

如果要将其他数据发送到Syslog,则除了%M 之外,还应使用RFC5424格式,并将其他信息发送为结构化数据。例如,您可以使用(以XML格式):

<Syslog name="sysLogger" host="localhost" port="514" protocol="UDP">
  <Rfc5424Layout appName="MyApp" facility="LOCAL1">
    <LoggerFields enterpriseId="32473" sdId="location">
      <KeyValuePair key="logger" value="%c" />
      <KeyValuePair key="class" value="%C" />
      <KeyValuePair key="method" value="%M" />
      <KeyValuePair key="line" value="%L" />
    </LoggerFields>
  </Rfc5424Layout>
</Syslog>

将属性格式转换为:

appender.syslog.type = Syslog
appender.syslog.name = sysLogger
appender.syslog.host = localhost
appender.syslog.port = 514
appender.syslog.protocol = UDP
appender.syslog.layout.type = Rfc5424Layout
appender.syslog.layout.facility = LOCAL1
appender.syslog.layout.appName = MyApp
appender.syslog.layout.fields.type = LoggerFields
appender.syslog.layout.fields.enterpriseId = 32473
appender.syslog.layout.fields.sdId = location
appender.syslog.layout.fields.0.type = KeyValuePair
appender.syslog.layout.fields.0.key = logger
appender.syslog.layout.fields.0.value = %c
...

几乎所有现代系统syslog服务器都可以解释结构化数据。对于rsyslog,您需要:

  1. 启用结构化数据解析:
     模块(load =“ mmpstrucdata”)
    动作(type =“ mmpstrucdata”)
     
  2. 创建一个模板以格式化您的消息:
      template(name =“ myAppformat” type =“ list”){
        属性(name =“ timereported” dateformat =“ rfc3339”)
        常数(value =“”)
        属性(名称=“主机名”)
        常数(value =“”)
        属性(名称=“ syslogtag”)
        常数(value =“”)
        属性(name =“ $!rfc5424-sd!位置@32473!class”)
        常数(value =“(”)
        属性(name =“ $!rfc5424-sd!位置@32473!方法”)
        常数(value =“:”)
        属性(name =“ $!rfc5424-sd!位置@32473!line”)
        常数(value =“)”)
        属性(name =“ msg” droplastlf =“ on”)
        常数(value =“ \ n”)
    }
     
  3. 使用模板来来自“ myapp”的消息:
     :app-name,iSequal,“ myApp” {
        /var/log/myapp.log; mymyappformat
        停止
    }
     

Log4j2's syslog layout is used to format the entire syslog message and must therefore be one of SyslogLayout (traditional BSD syslog format) or Rfc5424Layout (modern syslog layout). Using any other layout will result in invalid messages and RSyslog will have to guess the message's metadata. Most notably the facility will be set to USER.

If you want to send additional data to syslog, beyond %m, you should use the RFC5424 format and send the additional information as structured data. For example you can use (in XML format):

<Syslog name="sysLogger" host="localhost" port="514" protocol="UDP">
  <Rfc5424Layout appName="MyApp" facility="LOCAL1">
    <LoggerFields enterpriseId="32473" sdId="location">
      <KeyValuePair key="logger" value="%c" />
      <KeyValuePair key="class" value="%C" />
      <KeyValuePair key="method" value="%M" />
      <KeyValuePair key="line" value="%L" />
    </LoggerFields>
  </Rfc5424Layout>
</Syslog>

which translates to the properties format as:

appender.syslog.type = Syslog
appender.syslog.name = sysLogger
appender.syslog.host = localhost
appender.syslog.port = 514
appender.syslog.protocol = UDP
appender.syslog.layout.type = Rfc5424Layout
appender.syslog.layout.facility = LOCAL1
appender.syslog.layout.appName = MyApp
appender.syslog.layout.fields.type = LoggerFields
appender.syslog.layout.fields.enterpriseId = 32473
appender.syslog.layout.fields.sdId = location
appender.syslog.layout.fields.0.type = KeyValuePair
appender.syslog.layout.fields.0.key = logger
appender.syslog.layout.fields.0.value = %c
...

Virtually all modern syslog servers can interpret structured data. For RSyslog you need to:

  1. Enable structured data parsing:
    module(load="mmpstrucdata")
    action(type="mmpstrucdata")
    
  2. Create a template to format your message:
    template(name="MyAppFormat" type="list") {
        property(name="timereported" dateFormat="rfc3339")
        constant(value=" ")
        property(name="hostname")
        constant(value=" ")
        property(name="syslogtag")
        constant(value=" ")
        property(name="$!rfc5424-sd!location@32473!class")
        constant(value=" (")
        property(name="$!rfc5424-sd!location@32473!method")
        constant(value=":")
        property(name="$!rfc5424-sd!location@32473!line")
        constant(value=") ")
        property(name="msg" droplastlf="on")
        constant(value="\n")
    }
    
  3. Use the template for messages coming from "MyApp":
    :app-name, isequal, "MyApp" {
        /var/log/myapp.log;MyAppFormat
        stop
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文