在log4j2json配置中是否可以有注释
我使用JSON格式配置了log4j2,例如:在log4j2json配置中是否可以有注释,json,log4j2,Json,Log4j2,我使用JSON格式配置了log4j2,例如: { "configuration": { "monitorInterval": 60, "properties": { ... }, "appenders": { ... }, "loggers": { "asyncRoot": { ... 等等。 配置相当复杂,因此需要对将来试图理解或更改它的人进行一些澄清 如果这个配置是XML格式的,我可以
{
"configuration": {
"monitorInterval": 60,
"properties": {
...
},
"appenders": {
...
},
"loggers": {
"asyncRoot": {
...
等等。
配置相当复杂,因此需要对将来试图理解或更改它的人进行一些澄清
如果这个配置是XML格式的,我可以指定注释来澄清配置中的每个部分(将配置更改为XML不是一个选项)。但是JSON并没有注释作为一个概念,建议是。例如:
但我不确定在log4j2的情况下这样做是否是一个好主意:它没有使配置更清晰,而是实际上增加了一个大容量,这样的注释没有与实际数据明确分开,而且我不确定在解析配置时是否会导致任何稳定性/性能问题(如您所见,此配置每60秒分析一次,所以…)
是否有任何通用/推荐的方法来描述log4j2的配置?
或者您是否成功地测试/使用了上述方法(注释作为数据)
提前感谢。在
log4j2json
配置文件中可以有在线注释。您可以用以下方式编写注释-
{
"configuration": { // this is comment
"monitorInterval": 60, // set monitor interval here
"properties": {
...
},
"appenders": {
...
},
"loggers": { // set logger details here
"asyncRoot": {
...
这是因为log4j2
使用Jackson
解析JSON
配置文件,并且Jackson
具有在JSON
中启用注释的选项
尽管如此,以这种方式编写注释可能会在eclipse等IDE中显示错误,因为标准不支持注释。其他信息:Log4J2在以下位置显式启用注释: 它引用了
com.fasterxml.jackson.core
中的一个属性,该属性描述了支持哪些注释以及为什么:
/**
*确定解析器是否允许使用的特性
*Java/C++风格的注释(包括“/”+“*”和
*“//”变体)是否包含在已解析的内容中。
*
*因为JSON规范没有提到注释是合法的
*建设,,
*这是一个非标准特性;但是,在野外
*这是广泛使用的。因此,功能是
*默认情况下已禁用解析器,并且必须
*显式启用。
*/
允许注释(false)
Log4J中的任何地方都没有记录此功能,请注意,默认情况下Jackson已禁用此功能。perfect,它可以正常工作。希望未来版本的log4j2不会破坏此“意外功能”。
{
"configuration": { // this is comment
"monitorInterval": 60, // set monitor interval here
"properties": {
...
},
"appenders": {
...
},
"loggers": { // set logger details here
"asyncRoot": {
...
protected ObjectMapper getObjectMapper() {
return new ObjectMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true);
}
/**
* Feature that determines whether parser will allow use
* of Java/C++ style comments (both '/'+'*' and
* '//' varieties) within parsed content or not.
*<p>
* Since JSON specification does not mention comments as legal
* construct,
* this is a non-standard feature; however, in the wild
* this is extensively used. As such, feature is
* <b>disabled by default</b> for parsers and must be
* explicitly enabled.
*/
ALLOW_COMMENTS(false)