Log4j 2配置:XML vs JSON?

时间:2022-04-09 23:02:12

Apache manual simply states: "

Apache手册简单地说:“

Configuration of Log4j 2 can be accomplished in 1 of 4 ways:
Through a configuration file written in XML, JSON, or YAML.
Programmatically, by creating a ConfigurationFactory and Configuration implementation.
Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.
Programmatically, by calling methods on the internal Logger class.

Log4j 2的配置可以通过以下4种方式之一完成:通过用XML,JSON或YAML编写的配置文件。以编程方式,通过创建ConfigurationFactory和Configuration实现。以编程方式,通过调用Configuration接口中公开的API将组件添加到默认配置。以编程方式,通过调用内部Logger类上的方法。

Id' like to hear if there's visible trends in favoring one of these 4 ways?
Behind this question is the shift of Log4j (version 1) configuration practice from .properties files to XML format. It's pretty frustrating to search for answers for a problem and receive the right answer but not quite with configuration method you are using.

我想听听是否有明显的趋势支持这4种方式中的一种?这个问题的背后是将Log4j(版本1)配置实践从.properties文件转换为XML格式。搜索问题的答案并获得正确的答案却非常令人沮丧,但与您使用的配置方法完全不同。

I have a new log4j (version 2) project coming up and I'd like to use JSON as a configuration format against the default XML way of doing it (reason: aesthetics and slight performance advantage(?) ).

我有一个新的log4j(版本2)项目即将出现,我想使用JSON作为配置格式来对抗默认的XML方式(原因:美观和轻微的性能优势(?))。

Will I encounter problems or not get as many features if I choose the JSON configuration format over XML?

如果我选择基于XML的JSON配置格式,是否会遇到问题或者没有获得尽可能多的功能?

Any reasons to use programmatic approach instead of a static configuration file?

使用编程方法而不是静态配置文件的任何理由?

Use case would be (ideally) that single configuration file is read by the application server (from some file path on the server). This single .json file would then be used to apply log levels per application and produce at least 3 different logs (general, error, custom log level) per application. There's less than 10 applications designed to run on a server.

用例(理想情况下)是应用程序服务器(从服务器上的某个文件路径)读取单个配置文件。然后,此单个.json文件将用于为每个应用程序应用日志级别,并为每个应用程序生成至少3个不同的日志(常规,错误,自定义日志级别)。设计用于在服务器上运行的应用程序不到10个。

1 个解决方案

#1


6  

There is no performance advantage or disadvantage to using XML or JSON. Personally I prefer XML for these two reasons: (but they are not huge problems so of course it is up to you...)

使用XML或JSON没有性能优势或劣势。我个人更喜欢XML这两个原因:(但它们不是很大的问题所以当然这取决于你......)

  • JSON configuration requires the Jackson jar files in the classpath, so there's an extra dependency. XML uses the XML parser built in to Java.
  • JSON配置需要类路径中的Jackson jar文件,因此存在额外的依赖性。 XML使用Java内置的XML解析器。

  • All examples in the log4j2 manual use XML, not JSON, so you'll need to convert the syntax. If you choose XML you can copy-and-paste from the manual.
  • log4j2手册中的所有示例都使用XML,而不是JSON,因此您需要转换语法。如果选择XML,则可以从手册中进行复制和粘贴。

I would not recommend programmatic configuration. It is more complex and depends on implementation specifics, which may change in the future. IMHO it will be a bunch of work for little or no benefit.

我不建议编程配置。它更复杂,取决于具体的实施细节,将来可能会发生变化。恕我直言,这将是一堆很少或没有好处的工作。

For the use case you describe, you can use system properties in your configuration so multiple apps can share the same config but log to different locations by varying the property values for each application.

对于您描述的用例,您可以在配置中使用系统属性,以便多个应用程序可以共享相同的配置,但通过更改每个应用程序的属性值来登录到不同的位置。


UPDATE:

Log4j2 added an API for programmatic configuration in a later release. This API doesn’t expose implementation details. However it’s mostly suitable for configuring from scratch. If you want to modify an existing configuration you will need to use implementation classes.

Log4j2在以后的版本中添加了用于编程配置的API。此API不公开实现细节。但它最适合从头开始配置。如果要修改现有配置,则需要使用实现类。

#1


6  

There is no performance advantage or disadvantage to using XML or JSON. Personally I prefer XML for these two reasons: (but they are not huge problems so of course it is up to you...)

使用XML或JSON没有性能优势或劣势。我个人更喜欢XML这两个原因:(但它们不是很大的问题所以当然这取决于你......)

  • JSON configuration requires the Jackson jar files in the classpath, so there's an extra dependency. XML uses the XML parser built in to Java.
  • JSON配置需要类路径中的Jackson jar文件,因此存在额外的依赖性。 XML使用Java内置的XML解析器。

  • All examples in the log4j2 manual use XML, not JSON, so you'll need to convert the syntax. If you choose XML you can copy-and-paste from the manual.
  • log4j2手册中的所有示例都使用XML,而不是JSON,因此您需要转换语法。如果选择XML,则可以从手册中进行复制和粘贴。

I would not recommend programmatic configuration. It is more complex and depends on implementation specifics, which may change in the future. IMHO it will be a bunch of work for little or no benefit.

我不建议编程配置。它更复杂,取决于具体的实施细节,将来可能会发生变化。恕我直言,这将是一堆很少或没有好处的工作。

For the use case you describe, you can use system properties in your configuration so multiple apps can share the same config but log to different locations by varying the property values for each application.

对于您描述的用例,您可以在配置中使用系统属性,以便多个应用程序可以共享相同的配置,但通过更改每个应用程序的属性值来登录到不同的位置。


UPDATE:

Log4j2 added an API for programmatic configuration in a later release. This API doesn’t expose implementation details. However it’s mostly suitable for configuring from scratch. If you want to modify an existing configuration you will need to use implementation classes.

Log4j2在以后的版本中添加了用于编程配置的API。此API不公开实现细节。但它最适合从头开始配置。如果要修改现有配置,则需要使用实现类。