main ERROR Unable to locate appender "test" for logger config "test"
Asked Answered
D

1

6

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");
Damian answered 13/9, 2017 at 21:9 Comment(0)
P
4

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"
                    }
                }
            ]
        }
    }
}
Pussyfoot answered 14/9, 2017 at 9:23 Comment(1)
It works. Thanks a lot for providing the complete json config.Damian

© 2022 - 2024 — McMap. All rights reserved.