1
votes

I have a log4j2 json configuration file and it appears that I am unable to place multiple "File" Appenders in the Appender section. What I want to do is log two two files with my log statements: One file is for info level and higher while the other file is debug-level.

`"configuration": 
{       
    "appenders": 
    {
        "Console": {
            "name": "ConsoleAppender",
            "target": "SYSTEM_OUT",
            "PatternLayout": {
                "pattern": "%d [%t] %-5p %c - %m%n"
            }
        },
        "File": {
            "name":"FileAppenderInfo",
            "fileName":"/logs/info-logs.log",
            "PatternLayout": {
                "pattern":"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"
            }
        },
        "File": {
           "name": "FileAppenderDebug",
           "fileName": "/logs/debug-logs.log",
           "PatternLayout": {
                "pattern":"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"
            },
            "DefaultRolloverStrategy": {
                "max": "10"
            }
       }
    },
    "loggers": {
       "logger": {
            "name": "com.nf.eb2b.logging.DPLogger",
            "level": "debug",
            "appender-ref": [{"ref": "ConsoleAppender", "level":"info"},{"ref": "FileAppenderInfo", "level":"info"}, {"ref": "FileAppenderDebug", "level":"debug"}]
        },
        "root": {
            "AppenderRef": [{"ref": "ConsoleAppender"}]
        }
    }
}

}

What I would expect are two files in the logs directory. One for info level logs and the other for debug level logs. I want stdout to log info level logs to the console.

Instead I see that the editor does not accept two File appenders defined in the json and complains that there is a duplicate key. The other issue is that only one of the file appenders is recognized (The first one..)

How can I configure a single logger to write info-level logs to the console and to a file as well as a second file storing debug level messages.

1
Check following answer. It may help you.stackoverflow.com/questions/21512629/…parthivrshah
I would suggest you add "status": "debug" to the configuration so you can see what Log4j is doing.rgoers
Have you solved this?Denis

1 Answers

0
votes

You can use array ([]) of appenders inside File appender definition:

{
  "configuration": {
    "appenders": {
      "Console": {
        "name": "ConsoleAppender",
        "target": "SYSTEM_OUT",
        "PatternLayout": {
          "pattern": "%d [%t] %-5p %c - %m%n"
        }
      },
      "File": [
        {
          "name": "FileAppenderInfo",
          "fileName": "/logs/info-logs.log",
          "PatternLayout": {
            "pattern":"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"
          }
        },
        {
          "name": "FileAppenderDebug",
          "fileName": "/logs/debug-logs.log",
          "PatternLayout": {
            "pattern":"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"
          },
          "DefaultRolloverStrategy": {
            "max": "10"
          }
        }
      ]
    },
    "loggers": {
      "logger": [
        {
          "name": "InfoLogger",
          "level": "info",
          "AppenderRef": [
            {
              "ref": "FileAppenderInfo"
            }
          ]
        },
        {
          "name": "DebugLogger",
          "level": "debug",
          "AppenderRef": [
            {
              "ref": "FileAppenderDebug"
            }
          ]
        }
      ],
      "root": {
        "AppenderRef": [
          {
            "ref": "ConsoleAppender"
          }
        ]
      }
    }
  }
}