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

发布于 2024-08-14 18:46:23 字数 125 浏览 5 评论 0原文

我需要使用自定义过滤器,因此我需要将一些长 log4j.properties 文件转换为 log4j.xml。

有人知道有一种工具可以做到这一点,或者愿意贡献一个他们使用过的工具吗?到目前为止,搜索还没有找到这样的工具。

I need to use custom filters, so I need to convert some long log4j.properties files to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倒带 2024-08-21 18:46:23

我也需要这样做,但找不到工具。手动迁移数十个 log4j.properties 并不是一个令人满意的选择。因此,我开发了一个可以做到这一点的工具,并将其发布给其他人使用。

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

这有点粗糙,但是对我给它的 log4j 属性起到了作用,所以任何问题都会将它们记录在问题跟踪器上。希望您觉得它有用。

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. 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/

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. Hope you find it useful.

滥情稳全场 2024-08-21 18:46:23

以下内容可能对您有帮助。 Log4j 的创建者 Ceki Gülcü 启动了另一个名为 logback 的记录器项目,他提供了一个 log4j.properties 文件到 logback 的 xml 配置文件的在线翻译器。看起来 log4j.xml 和 logback.xml 的配置文件模式非常接近。

至少它应该生成可以轻松转换为 log4j.xml 格式的内容。

为了您的方便:这是 log4j 文档中的示例 log4j.properties 文件。只需将其粘贴到翻译器中并检查输出:

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

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. It looks like the configuration file schemas of log4j.xml and logback.xml are pretty close.

At least it should produce something that can easily be converted to the log4j.xml format.

For your convenience: here's a sample log4j.properties file from the log4j documentation. 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
陌上芳菲 2024-08-21 18:46:23

迟到的答案,但现在 class 提供从 log4jv1 .properties 到 log4jv2.xml 的批量(递归)转换配置。

来自 javadocs:

用于将 Log4j 1.x 属性配置文件转换为 Log4j 2.x XML 配置文件的工具。

为了转换在给定路径中递归找到的所有名为 log4j.properties 的文件:

  • 下载 二进制分发版并将其解压缩到文件夹中(例如 bin),

  • 运行:

    java -cp "bin/*" org.apache.log4j.config.Log4j1ConfigurationConverter \
      --recurse <要转换的目录路径> \
      --在 log4j.properties \
      --详细
    

Late answer, but nowadays a class named Log4j1ConfigurationConverter from the log4j bridge API offers batch(recursive) conversion from log4jv1 .properties to log4jv2.xml config.

From the javadocs:

Tool for converting a Log4j 1.x properties configuration file to Log4j 2.x XML configuration file.

In order to convert all files named log4j.properties found recursively in a given path:

  • Download the binary distribution from Log4j's site and unzip it in a folder (e.g. bin),

  • Run:

    java -cp "bin/*" org.apache.log4j.config.Log4j1ConfigurationConverter \
      --recurse <directory-path-to-convert> \
      --in log4j.properties \
      --verbose
    
妄断弥空 2024-08-21 18:46:23

想必您必须有很多需要转换的属性文件。当我想切换到 xml 时(很多年前),我找不到太多文档。我确实在 log4j 发行版中找到了非常有用的示例 xml 文件。

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. I did find sample xml files in the log4j distribution which were very useful.

兲鉂ぱ嘚淚 2024-08-21 18:46:23

最近使用 https://github.com/balent/Log4j-Convert-Configuration 来将 log4j.xml 文件转换为 log4j.properties 文件(我必须使用一个古老的 jar 来明确查找 log4j.properties)。配置允许采用两种方式:xml 到属性或属性到 xml。

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

将其保留为开这里供将来的我找到。

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). The configuration allows going both ways: xml to properties OR properties to 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:

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

Leaving this on here for future me to find.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文