简体   繁体   English

Log4j记录信息消息而不记录警告消息

[英]Log4j logging info messages without logging warn messages

I'm trying to log certain info messages onto a file but as soon as I run the application both warn and info messages are logged. 我试图将某些信息消息记录到文件中,但是一旦运行该应用程序,就会同时记录警告和信息消息。 Now, from what I've read from this site , you cannot log one without logging the other. 现在,根据我从本站点所读到的内容,您不能登录一个而不登录另一个。 Has anyone tried this before? 有人曾尝试过这个吗? If so, how did your properties file look like? 如果是这样,您的属性文件的外观如何?

My properties file looks like this: 我的属性文件如下所示:

 ***** Set root logger level to INFO and its two appenders to stdout and R.
log4j.rootLogger=INFO, stdout, R

# ***** stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# ***** stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# ***** Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%M has started] (%F:%L) - %m%n
/
# ***** R is set to be a RollingFileAppender.
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.R.File="folder where log will be saved"
log4j.appender.R.layout.ConversionPattern=%5p [%m has started] %c{2}.[%x] (%F:%L) %d{yyyy-MM-dd HH:mm:ss} - %m%n

# ***** R uses PatternLayout.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%5p [%m has started] %c{2}.[%x] (%F:%L) %d{yyyy-MM-dd HH:mm:ss} - %m%n

AFAIK there's no standard way to suppress higher log levels than those you are interested in. AFAIK没有任何标准方法可以抑制比您感兴趣的日志级别更高的日志级别。

However, you might be able to use a custom appender to do that. 但是,您可能可以使用自定义附加程序执行此操作。

It might look similar to this: 它可能看起来类似于:

public class MyAppender extends AppenderSkeleton {
  protected void append(LoggingEvent event) {
    if( event.getLevel() == Level.INFO ) {
      //append here, maybe call a nested appender
    }
  }
}

The log level WARN is higher than INFO , and the logging configuration defines the minimum threshold level to be logged by the appender. 日志级别WARN高于INFO ,并且日志记录配置定义了附加程序要记录的最小阈值级别。 So, all messages higher than that level will also be logged. 因此,所有高于该级别的消息也将被记录。

Hence, the WARN messages are expected. 因此,期望WARN消息。 And I don't think you can configure it to the way you want. 而且我认为您无法将其配置为所需的方式。

If the WARN messages that should not be printed come from a different package than the INFO messages, then you can define different log levels for these packages. 如果不应该打印的WARN消息与INFO消息来自不同的软件包,则可以为这些软件包定义不同的日志级别。 The first can specify level ERROR and the second INFO 第一个可以指定错误级别,第二个可以指定级别

it should look something like this: log4j.logger.com.test.something=ERROR log4j.logger.com.other.package=INFO 它应该看起来像这样:log4j.logger.com.test.something = ERROR log4j.logger.com.other.package = INFO

cheers 干杯

Why do you want to filter the WARN level? 为什么要过滤WARN级别? As peshkira said, you could use two different loggers for splitting/filtering your log output, or you could use tools like grep for filtering (offline) or you could simply remove the WARN logs from your code if your don't need them anyway. 正如peshkira所说,您可以使用两个不同的记录器来拆分/过滤日志输出,或者可以使用grep工具进行过滤(脱机),或者如果您仍然不需要它们,则可以从代码中删除WARN日志。

As far as I understand, advanced filters like LevelMatchFilter and LevelRangeFilter can do the trick for you. 据我了解,像LevelMatchFilterLevelRangeFilter这样的高级过滤器可以为您解决问题。

Thing worth keeping in mind though is that using these may require xml config instead of properties: Can't set LevelRangeFilter for log4j 但是需要记住的是,使用这些可能需要xml配置而不是属性: 无法为log4j设置LevelRangeFilter

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

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