主ERROR无法找到记录器配置“测试”的appender“test”

时间:2023-02-05 22:03:07

this is my log4j2 json config

这是我的log4j2 json配置

{
"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": "test",
    "fileName": "c:/logs/requests_received.log"
  },
  "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 dont want to use a rolling file appender.

当我使用一个文件appender它工作正常。当我使用两个文件appender获取title.cant中的错误提示时,我们添加了多个文件appender。我不想使用滚动文件appender。

please ignore this "It looks like your post is mostly code; please add some more details. what more details you want? i think i have conveyed everything. what else you want me to add. let the people look at the issue."

请忽略这一点“看起来您的帖子主要是代码;请添加更多详细信息。您想要更多详细信息?我想我已经传达了所有内容。您还希望我添加什么。让人们看看问题。”

Java code for creating logger variables

用于创建记录器变量的Java代码

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 个解决方案

#1


2  

Try to define your appenders using type attribute like below -

尝试使用类型属性定义您的appender,如下所示 -

{
    "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 -

为此,您必须使用type属性修改所有appender配置。您的完整配置文件将更改如下 -

{
    "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"
                    }
                }
            ]
        }
    }
}

#1


2  

Try to define your appenders using type attribute like below -

尝试使用类型属性定义您的appender,如下所示 -

{
    "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 -

为此,您必须使用type属性修改所有appender配置。您的完整配置文件将更改如下 -

{
    "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"
                    }
                }
            ]
        }
    }
}