简体   繁体   English

log4j-错误,登录调试?

[英]log4j - on ERROR, log DEBUG?

I'm relatively new to log4j (v2), but I believe I've exhausted my resources, so I come to ask my question here. 我对log4j(v2)相对较新,但是我相信我已经用光了资源,因此我在这里提出我的问题。

I have a working log4j configuration, which I will append to the end of my question. 我有一个可运行的log4j配置,我将在问题末尾附加该配置。 The application is run every x minutes using a cronjob. 该应用程序使用cronjob每x分钟运行一次。

The log4j behaviour I'd like to see is the following: 我想看到的log4j行为如下:

  1. All INFO (and up) logging is done to file A 所有INFO(及以上)日志记录都已完成到文件A
  2. All ERROR (and up) logging is done to file B 对文件B的所有错误(及以上记录)都已记录
  3. In case of an ERROR (or up), a mail is sent containing the DEBUG (and up) lines of the given execution. 如果发生错误(或以上),则会发送一封包含给定执行的DEBUG(及以上)行的邮件。

I have achieved 1 and 2 using the configuration below. 我使用以下配置实现了1和2。 Is number 3 also possible using log4j (if not, why not?) and if so, how? 使用log4j是否也可以使用3号(如果不是,为什么呢?),如果可以,怎么办?

The current configuration: 当前配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <RandomAccessFile name="LogFile" fileName="/var/log/fileA.log">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </RandomAccessFile>
    <RandomAccessFile name="ErrorLogFile" fileName="/var/log/fileB.log">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </RandomAccessFile>
    <SMTP name="MailError" subject="Error occurred" to="a@b.com" smtpHost="mail.foo.bar" from="b@c.net"/>
    <Async name="Async">
      <AppenderRef ref="LogFile"/>
    </Async>
    <Async name="Errors">
      <AppenderRef ref="ErrorLogFile"/>
      <AppenderRef ref="MailError"/>
    </Async>
  </Appenders>
  <Loggers>
    <Root level="DEBUG">
      <AppenderRef ref="Console" level="INFO"/>
      <AppenderRef ref="Async" level="INFO"/>
      <AppenderRef ref="Errors" level="ERROR"/>
    </Root>
  </Loggers>
</Configuration>

I think you should be able to achieve this with a ThresholdFilter . 我认为您应该可以使用ThresholdFilter实现此目的。 Make your root logger level TRACE so that it sends all events to all appenders, then put different threshold filters on each appender. 将您的根记录程序级别设置为TRACE,以便将所有事件发送到所有附加程序,然后在每个附加程序上放置不同的阈值过滤器。 The log4j2 manual contains an example. log4j2手册包含一个示例。


(EDIT) After understanding the requirements better, I think the SMTP Appender already does what you need: it internally keeps a buffer of the events (512 by default) that preceded the ERROR event that triggered the email. (编辑)在更好地理解了需求之后,我认为SMTP Appender已经满足了您的需要:它在内部保留了触发ERROR事件的事件(默认为512)的缓冲区。 These preceding events will be included in the email. 这些先前的事件将包含在电子邮件中。

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

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