-1

I have log4j2 configured using JSON format, e.g.:

{
  "configuration": {
    "monitorInterval": 60,
    "properties": {
      ...
    },
    "appenders": {
      ...
    },

    "loggers": {
      "asyncRoot": {
        ...

etc. The configuration is quite complex, so it requires some clarifications for whoever attempts to understand or change it in the future.

If this configuration was in XML, I could specify comments clarifying each section right in the configuration (changing my configuration to XML is not an option). But JSON doesn't have comments as a concept, and recommendation is to specify comments as data. E.g.:

{
  "configuration": {
    "monitorInterval": 60,
    "_comment_": "here we go",
    "properties": {

But I'm not sure if something like that is a good idea in log4j2 case: instead of making configuration more clear, it actually adds a bulk to it, such comments are not clearly separated from actual data, and I am not sure if it will cause any stability / performance issues parsing configuration (as you can see, this configuration is parsed every 60 sec, so...)

Is there any common / recommended approach to describe the configuration with log4j2? Or did you successfully test/use the above approach (comments as data)?

Thanks in advance.

timbre timbre
  • 12,648
  • 10
  • 46
  • 77

2 Answers2

1

It is possible to have in-line comments in log4j2 JSON configuration file. You can write comment in below way -

{
"configuration": {           // this is comment
"monitorInterval": 60,       // set monitor interval here
"properties": {
  ...
},
"appenders": {
  ...
},

"loggers": {                // set logger details here
  "asyncRoot": {
    ...

It is because log4j2 uses Jackson for parsing JSON configuration file and Jackson has option to enable comments in JSON.

Although, writing comments in this way may show you error in IDE like eclipse as JSON standard does not support comments.

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
0

Additional information: Log4J2 enables comments explicitly in JsonConfiguration implementation:

protected ObjectMapper getObjectMapper() {
    return new ObjectMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true);
}

It refers to a property from com.fasterxml.jackson.core, which describes details on which comments are supported and why:

/**
* 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)

Feature is not documented anywhere in Log4J, also notice that by default Jackson has it disabled.

timbre timbre
  • 12,648
  • 10
  • 46
  • 77