简体   繁体   English

将 log4j.properties 转换为 log4j.xml 的脚本

[英]Script to convert log4j.properties to log4j.xml

I need to use custom filters, so I need to convert some long log4j.properties files to log4j.xml.我需要使用自定义过滤器,所以我需要将一些长的 log4j.properties 文件转换为 log4j.xml。

Is anyone aware of a tool to do this, or willing to contribute one they have used?有没有人知道这样做的工具,或者愿意贡献他们使用过的工具? Searching has thus far turned up no such tool.到目前为止,搜索还没有找到这样的工具。

I needed to do this as well, but couldn't find a tool. 我也需要这样做,但找不到工具。 Migrating by hand dozens of log4j.properties was not a palatable option. 手动迁移数十个log4j.properties并不是一个可口的选择。 So, I bashed together a tool that can do it and released it for others to use. 所以,我把一个可以做到的工具混合在一起并发布给其他人使用。

http://code.google.com/p/log4j-properties-converter/ http://code.google.com/p/log4j-properties-converter/

It's a bit rough but did the trick for the log4j properties I gave it, so any problems log them on the issue tracker. 它有点粗糙但是我给它的log4j属性的技巧,所以任何问题都记录在问题跟踪器上。 Hope you find it useful. 希望你觉得它有用。

Here is something that may help you. 可能对您有所帮助。 Ceki Gülcü, the creator of log4j, started another logger project named logback , and he provides an online translator for log4j.properties files to xml config files for logback. log4j的创建者CekiGülcü启动了另一个名为logback的记录器项目,他为log4j.properties文件提供了一个在线翻译器,用于xml配置文件以进行记录。 It looks like the configuration file schemas of log4j.xml and logback.xml are pretty close. 看起来log4j.xml和logback.xml的配置文件模式非常接近。

At least it should produce something that can easily be converted to the log4j.xml format. 至少它应该生成一些可以很容易地转换为log4j.xml格式的东西。

For your convenience: here's a sample log4j.properties file from the log4j documentation. 为方便起见:这是log4j文档中的示例log4j.properties文件。 Just paste it into the translator and check the output: 只需将其粘贴到翻译器中并检查输出:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
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 [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Late answer, but nowadays a class named Log4j1ConfigurationConverter from the log4j bridge API offers batch(recursive) conversion from log4jv1.properties to log4jv2.xml config.迟到的答案,但如今来自 log4j 桥API的名为Log4j1ConfigurationConverter的 class 提供从 log4jv1.properties 到 log4jv2.xml 配置的批量(递归)转换。

From the javadocs: "Tool for converting a Log4j 1.x properties configuration file to Log4j 2.x XML configuration file."来自 javadocs: “用于将 Log4j 1.x 属性配置文件转换为 Log4j 2.x XML 配置文件的工具。”

Example call that converts all files named log4j.properties found recursively in a given path:转换在给定路径中递归找到的名为 log4j.properties 的所有文件的示例调用:

 java org.apache.log4j.config.Log4j1ConfigurationConverter --recurse
 <directory-path-to-convert> --in log4j.properties --verbose

Presumably you must have a lot of properties files you need to convert. 据推测,您必须拥有许多需要转换的属性文件。 When I wanted to switch to xml (many years ago) I couldn't find much documentation. 当我想切换到xml(多年前)时,我找不到太多文档。 I did find sample xml files in the log4j distribution which were very useful. 我确实在log4j发行版中找到了非常有用的示例xml文件。

Recently used https://github.com/balent/Log4j-Convert-Configuration to convert a log4j.xml file to a log4j.properties file (an ancient jar I had to use was explicitly looking for log4j.properties). 最近使用https://github.com/balent/Log4j-Convert-Configuration将log4j.xml文件转换为log4j.properties文件(我必须使用的古代jar显式查找log4j.properties)。 The configuration allows going both ways: xml to properties OR properties to xml. 配置允许双向:xml到属性或属性到xml。

Download the binary build from https://github.com/balent/Log4j-Convert-Configuration/blob/master/binaries/full-with-dependencies/log4j-convert-full.jar and then just type: https://github.com/balent/Log4j-Convert-Configuration/blob/master/binaries/full-with-dependencies/log4j-convert-full.jar下载二进制版本,然后输入:

java -jar log4j-convert-full.jar -i log4j.properties -o log4j.xml

Leaving this on here for future me to find. 离开这里,以便将来我找到。

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

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