简体   繁体   English

如何使用 log4j 和 log4j2 属性文件登录到同一文件

[英]How to use log4j and log4j2 properies file to log into same file

I want to roll and compress log files based on size and time .我想根据大小时间滚动和压缩日志文件。 I have found out that this can easily be performed with log4j2.我发现使用 log4j2 可以很容易地执行此操作。 The problem is many internal jars still use log4j for logging, which I want to be logged.问题是许多内部 jars 仍然使用 log4j 进行日志记录,我想记录下来。

The possible solution I can think of here is by using log4j1.compatibility=true by keeping log4j.properties and log4j2.properties我在这里想到的可能的解决方案是通过使用log4j1.compatibility=true通过保持 log4j.properties 和 log4j2.properties

What I can't figure out is how do I use RollingFileAppender in log4j.properties and log4j2 properties?我想不通的是如何在 log4j.properties 和 log4j2 属性中使用 RollingFileAppender?

Please suggest me how can I use both the properties file(log4j and log4j2) to log to the same file with automatic compression when the specific size or time is reached.请建议我如何使用属性文件(log4j 和 log4j2)在达到特定大小或时间时通过自动压缩记录到同一个文件。

Note: I tried using uk.org.simonsite.log4j.appender.TimeAndSizeRollingAppender but getting error NoClassDefError: org.apache.log4j.xml.unrecognizedelementhandler due to class is removed in log4j versions greater than v1.2.17. Note: I tried using uk.org.simonsite.log4j.appender.TimeAndSizeRollingAppender but getting error NoClassDefError: org.apache.log4j.xml.unrecognizedelementhandler due to class is removed in log4j versions greater than v1.2.17.

Most libraries only use Log4j 1.x to log messages, they don't configure the logging system programmatically.大多数库只使用 Log4j 1.x 来记录消息,它们不以编程方式配置日志系统。 Therefore you can simply replace log4j with log4j-1.2-api .因此,您可以简单地将log4j替换为log4j-1.2-api

The Log4j 1.x bridge ( log4j-1.2-api ) shares its configuration with Log4j 2.x Core, so you can use a native Log4j 2.x configuration file to configure a RollingFileAppender . Log4j 1.x 桥 ( log4j-1.2-api ) 与 Log4j 2.x Core 共享其配置,因此您可以使用本机 Log4j 2.x 配置文件来配置RollingFileAppender No log4j.properties file or log4j1.compatibility property required.不需要log4j.properties文件或log4j1.compatibility属性。

PS: Out of 4 configuration file formats supported by Log4j 2.x (cf. documentation ) the properties format is probably the more convoluted. PS:在 Log4j 2.x(参见文档)支持的 4 种配置文件格式中,属性格式可能更为复杂。 Personally I would choose one of the remaining 3 (XML, JSON or YAML).我个人会选择其余 3 个之一(XML、JSON 或 YAML)。 The XML format does not require additional dependencies. XML 格式不需要额外的依赖。

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

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