main ERROR Unable to locate appender "test" for logger config "test"

12.1k views Asked by At

This is my log4j2 `JSON config

{
  "Configuration": {
    "Appenders": {
      "Console": {
        "PatternLayout": {
          "pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
        },
        "name": "Console",
        "target": "SYSTEM_OUT"
      },
      "RollingFile": {
        "name": "general",
        "fileName": "C:/logs/simulator-log.log",
        "filePattern": "C:/logs/simulator-log-%d{yyyy-MM-dd HH-mm-ss}.log",
        "PatternLayout": {
          "pattern": "%msg%n"
        },
        "Policies": {
          "OnStartupTriggeringPolicy": {}
        }
      },
      "File": {
        "PatternLayout": {
          "pattern": "%msg%n"
        },
        "name": "test1",
        "fileName": "c:/logs/response_sent.log"
      }
    },
    "Loggers": {
      "Root": {
        "AppenderRef": [
          {
            "ref": "Console"
          }
        ],
        "level": "trace"
      },
      "logger": [
        {
          "name": "test",
          "level": "trace",
          "additivity": "false",
          "AppenderRef": {
            "ref": "test"
          }
        },
        {
          "name": "test1",
          "level": "trace",
          "additivity": "false",
          "AppenderRef": {
            "ref": "test1"
          }
        },
        {
          "name": "general",
          "level": "trace",
          "additivity": "false",
          "AppenderRef": {
            "ref": "general"
          }
        }
      ]
    }
  }
}

when I use one file appenders it works fine. when I use two file appenders getting the error mention in the title.cant we add more than 1 file appenders. I don't want to use a rolling file appender.

Java code for creating logger variables

private static final Logger requestsReceived = LogManager.getLogger("test");
private static final Logger responseSent = LogManager.getLogger("test1");
private static final Logger logger = LogManager.getLogger("general");
1

There are 1 answers

1
Vikas Sachdeva On BEST ANSWER

Try to define your appenders using type attribute like below -

{
    "type": "File",
    "PatternLayout": {
        "pattern": "%msg%n"
    },
    "name": "test",
    "fileName": "c:/logs/requests_received.log"
},
{
    "type": "File",
    "PatternLayout": {
        "pattern": "%msg%n"
    },
    "name": "test1",
    "fileName": "c:/logs/response_sent.log"
}

For this, You have to modify all appender configuration using type attribute. Your complete configuration file will change like below -

{
    "Configuration": {
        "Appenders": {
            "appender": [
                {
                    "type": "Console",
                    "PatternLayout": {
                        "pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
                    },
                    "name": "Console",
                    "target": "SYSTEM_OUT"
                },
                {
                    "type": "RollingFile",
                    "name": "general",
                    "fileName": "C:/logs/simulator-log.log",
                    "filePattern": "C:/logs/simulator-log-%d{yyyy-MM-dd HH-mm-ss}.log",
                    "PatternLayout": {
                        "pattern": "%msg%n"
                    },
                    "Policies": {
                        "OnStartupTriggeringPolicy": {

                        }
                    }
                },
                {
                    "type": "File",
                    "PatternLayout": {
                        "pattern": "%msg%n"
                    },
                    "name": "test",
                    "fileName": "c:/logs/requests_received.log"
                },
                {
                    "type": "File",
                    "PatternLayout": {
                        "pattern": "%msg%n"
                    },
                    "name": "test1",
                    "fileName": "c:/logs/response_sent.log"
                }
            ]
        },
        "Loggers": {
            "Root": {
                "AppenderRef": [
                    {
                        "ref": "Console"
                    }
                ],
                "level": "trace"
            },
            "logger": [
                {
                    "name": "test",
                    "level": "trace",
                    "additivity": "false",
                    "AppenderRef": {
                        "ref": "test"
                    }
                },
                {
                    "name": "test1",
                    "level": "trace",
                    "additivity": "false",
                    "AppenderRef": {
                        "ref": "test1"
                    }
                },
                {
                    "name": "general",
                    "level": "trace",
                    "additivity": "false",
                    "AppenderRef": {
                        "ref": "general"
                    }
                }
            ]
        }
    }
}