java 从 log4j 迁移到 log4j2 - 属性文件配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35900555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Migrating from log4j to log4j2 - properties file configuration
提问by Raju Rao
I have a java application which is using log4jconfigured as below.
我有一个使用log4j的 java 应用程序,配置如下。
log4j.properties
log4j.properties
log4j.rootLogger=INFO, R
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = /trace.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{HH:mm:ss} %c{1} [%p] %m%n
I would like to migrate to log4j2 with the same configuration as above. Haven't found anything related to log4j2 properties configuration file as this support recently included.
我想使用与上述相同的配置迁移到 log4j2。没有发现与 log4j2 属性配置文件相关的任何内容,因为最近包含此支持。
Please can anyone help me out how would be my log4j2.propertiesfile with the same configuration above ?
请谁能帮我看看我的log4j2.properties文件如何与上面的配置相同?
采纳答案by Raju Rao
rootLogger.level = INFO
property.filename = trace.log
appenders = R, console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d %5p [%t] (%F:%L) - %m%n
appender.R.type = RollingFile
appender.R.name = File
appender.R.fileName = ${filename}
appender.R.filePattern = ${filename}.%d{yyyy-MM-dd}
appender.R.layout.type = PatternLayout
appender.R.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
appender.R.policies.type = Policies
appender.R.policies.time.type = TimeBasedTriggeringPolicy
appender.R.policies.time.interval = 1
rootLogger.appenderRefs = R, console
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.R.ref = File
回答by Navin Israni
You can use this to convert from Log4J.properties (v1.2) to log4j2.xml as below:
您可以使用它从 Log4J.properties (v1.2) 转换为 log4j2.xml,如下所示:
1) Convert from v1.2 properties to v1.2XML using this converter: https://log4j-props2xml.appspot.com/
1) 使用此转换器从 v1.2 属性转换为 v1.2XML:https://log4j-props2xml.appspot.com/
2) Convert from v1.2 XML to v2.0 XML (i.e. Log4j2.xml) using the procedure provided on this link: https://logging.apache.org/log4j/2.x/manual/migration.html
2) 使用此链接提供的程序从 v1.2 XML 转换为 v2.0 XML(即 Log4j2.xml):https://logging.apache.org/log4j/2.x/manual/migration.html
回答by Iwavenice
Log4j2 supports .properties files but they have changed property syntax. You can check their manual hereit covers all you need to create new configuration.
Log4j2 支持 .properties 文件,但它们更改了属性语法。您可以在此处查看他们的手册,它涵盖了创建新配置所需的所有内容。
回答by Artem Krosheninnikov
I know it's an old issue but for the sake of history:
我知道这是一个老问题,但为了历史:
Since Log4j2 2.13.0 there is an experimentalfeature for Log4j 1 configuration files: http://logging.apache.org/log4j/2.x/manual/compatibility.html
由于 Log4j2 2.13.0 有一个Log4j 1 配置文件的实验性功能:http: //logging.apache.org/log4j/2.x/manual/compatibility.html
Related JIRA issue: https://issues.apache.org/jira/browse/LOG4J2-63
相关 JIRA 问题:https: //issues.apache.org/jira/browse/LOG4J2-63
回答by ΦXoc? ? Пepeúpa ツ
You can use this wonderful frontend web to convert you properties to a XML http://log4j-props2xml.appspot.com/
您可以使用这个美妙的前端 Web 将您的属性转换为 XML http://log4j-props2xml.appspot.com/
Resulting:
结果:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="R" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="File" value="/trace.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss} %c{1} [%p] %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="R"/>
</root>
</log4j:configuration>