简体   繁体   English

Log4j:如何编写配置?

[英]Log4j: how to compose config?

I added Log4j2 to the project, and added config.我将 Log4j2 添加到项目中,并添加了配置。

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Than, I log code like here:比,我在这里记录代码:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

...

final static Logger logger = LogManager.getLogger(AbstractEditor.class);

...

logger.info("updated: " + entity);
logger.debug("==> debug");
logger.info("==> info");
logger.warn("==> warn");
logger.error("==> error");
logger.fatal("==> fatal");
logger.trace("==> trace");

As I understand, all logs with level higher than DEBUG must be written to console and file.据我了解,所有级别高于 DEBUG 的日志都必须写入控制台和文件。 But only this has been printed into console:但只有这个被打印到控制台:

15:08:52.285 [http-nio-8080-exec-1] ERROR ru.example.AbstractEditor - ==> error
15:08:52.292 [http-nio-8080-exec-1] FATAL ru.example.AbstractEditor - ==> fatal

I see this strings not matches my config.我看到这个字符串与我的配置不匹配。 And they are not witten into file.并且他们没有被写入文件。 When I added this config, all logs disappeared from console, excluding this 2 strings.当我添加这个配置时,所有日志都从控制台消失了,不包括这 2 个字符串。

Please help to write config to see all logs with level from DEBUG on console and file.请帮助编写配置以查看控制台和文件上调试级别的所有日志。

You are programmatically using Log4j 2 but using the configuration format of Log4j 1. Log4j 2 is ignoring your configuration and using the default.您以编程方式使用 Log4j 2,但使用 Log4j 1 的配置格式。 Log4j 2 忽略您的配置并使用默认值。 You have 2 choices.你有2个选择。

  1. Convert your configuration to use Log4j 2 syntax (recommended), or将您的配置转换为使用 Log4j 2 语法(推荐),或
  2. Enable Log4j 2's experimental support for log4j 1 configuration files by setting the system property "log4j1.compatibility=true".通过设置系统属性“log4j1.compatibility=true”,启用 Log4j 2 对 log4j 1 配置文件的实验性支持。 This support was added in release 2.13.0 so you would have to be using that version.此支持是在 2.13.0 版中添加的,因此您必须使用该版本。 The property file would also have to be named log4j.properties, not log4j2.properties.属性文件也必须命名为 log4j.properties,而不是 log4j2.properties。

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

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