简体   繁体   English

将来自现有log4j附加程序的日志将调试级别为“错误”或更高级别的所有日志写入rsyslog

[英]Write logs from existing log4j appenders all logs with debug level “error” or higher to rsyslog

In our java project, we send logs to all kinds of appenders. 在我们的java项目中,我们将日志发送到各种附加程序。 How do I log to rsyslog all logs that are written to those appenders from "error" level and higher by only changing configuration files such as log4j.xml (without meddling with the code)? 如何通过仅更改配置文件(例如log4j.xml)(不干预代码)来将写入“错误”级别及更高级别的那些追加程序的所有日志记录到rsyslog?

in How to log error and info messages separately into syslog with log4j? 如何使用log4j将错误和信息消息分别记录到syslog中? , there is an explanation how to create a new appender, and from my understanding to follow up with that answer I need to touch the code. ,这里有一个说明,说明如何创建新的附加程序,根据我的理解,按照该答案进行操作,我需要触摸代码。

my log4j version is: 2.4.1 Here is a small log4j.xml for one of the machines we wrote: 我的log4j版本是:2.4.1这是我们编写的其中一台机器的小型log4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="5">
  <Appenders>
      <RollingRandomAccessFile name="RollingRandomAccessFile" fileName="log/la.full.log" filePattern="log/la.full.%d{yy-dd-MM}.%i.log">
      <PatternLayout>
        <Pattern>%highlight{%d{DEFAULT}|%5p|%6t|%X|%m%n}</Pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="10 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="100"/>
    </RollingRandomAccessFile>
    <Console name="STDOUT" target="SYSTEM_OUT">
     <PatternLayout pattern="%highlight{%d{DEFAULT}|%5p|%6t|%X|%m%n}"/>
   </Console>
  </Appenders>
  <Loggers>
    <Root level="debug" additivity="false">
      <AppenderRef ref="STDOUT" level="DEBUG"/>
      <AppenderRef ref="RollingRandomAccessFile" level="DEBUG"/>
    </Root>
  </Loggers>
</Configuration>

Given you add an Appender for rsyslog ( which should be available from somewhere on the internet if not already built-in ) you just would add another appender-ref: 给您添加一个用于rsyslog的Appender(如果尚未内置,则可以从Internet上的某个地方获取),您只需添加另一个appender-ref:

<Loggers>
    <Root level="debug" additivity="false">
      <AppenderRef ref="STDOUT" level="DEBUG"/>
      <AppenderRef ref="RollingRandomAccessFile" level="DEBUG"/>
      <AppenderRef ref="RSYSLOG" level="ERROR"/>
    </Root>
</Loggers>

Concerning loglevel: Setting the level on Root logger will "filter" the messages going through to debug and higher ( debug, info, error, ... ). 关于日志级别:在Root logger上设置level将“过滤”正在调试的消息以及更高level的消息(debug,info,error等)。 Setting it also on the appender will further filter to that level (or higher). 在附加器上也进行设置会进一步过滤到该级别(或更高)。 So if you set the level property on the appender-ref to "error" (as shown above) it will only get messages in level error and higher. 因此,如果将appender-ref的level属性设置为“ error”(如上所示),它将仅获得级别为error或更高级别的消息。 The other appender won't be affected by that and still log debug and info messages. 另一个附加程序将不受此影响,并且仍会记录调试和信息消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM