从现有的log4j应用程序中写入日志,将所有调试级别为 "错误 "或更高的日志写入rsyslog。[英] Write logs from existing log4j appenders all logs with debug level "error" or higher to rsyslog

本文是小编为大家收集整理的关于从现有的log4j应用程序中写入日志,将所有调试级别为 "错误 "或更高的日志写入rsyslog。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我们的Java项目中,我们将日志发送给各种附录.如何通过更改诸如log4j.xml之类的配置文件(不处理代码)来登录所有将所有日志从"错误"级别和更高的附加器写给这些附录的所有日志?

?

in 如何使用log4j?将错误和信息消息分别记录到syslog?,有一个解释如何创建一个新的Appender,并且从我的理解中以跟进该答案,我需要触摸代码.

我的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>

推荐答案

允许您为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>

关于loglevel:在root Logger上设置level将"过滤"消息通过调试和更高的消息(调试,信息,错误,...).也将其设置在Appender上,将进一步过滤到该级别(或更高).因此,如果将appender-ref上的级别属性设置为"错误"(如上所示),它将只会在级别错误和更高的情况下获取消息.另一个附件不会受到该影响的影响,并且仍会记录调试和信息消息.

本文地址:https://www.itbaoku.cn/post/1575126.html

问题描述

In our java project, we send logs to all kinds of appenders. 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)?

in How to log error and info messages separately into syslog with log4j?, 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:

<?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:

<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, ... ). 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. The other appender won't be affected by that and still log debug and info messages.