log4j2.json如果没有名称就不能配置错误记录器:arg[2](null)?

mpgws1up  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(528)

这是我的log4j2.json文件,放在spring的resources文件夹中。我不明白如何在控制台和文件中同时打印。控制台输出正常,创建了.html文件,但没有使用

{
   "configuration": {
      "name": "Default",
      "appenders": {
         "Console": {
            "name": "STDOUT",
            "PatternLayout": {
               "pattern": "%d [%t] %-5p %c - %m%n"
            }
         },
         "RollingFile": {
            "name": "File",
            "fileName": "C:/Users/Tiziano/Desktop/LogFile/info-app.html",
            "filePattern": "C:/myppInfo-%d{MM-dd-yy-HH-mm-ss}-%i.html.gz",
            "HTMLLayout": {
               "charset": "UTF-8",
               "title": "Info Logs",
               "locationInfo": "true"
            },
            "Policies": {
               "SizeBasedTriggeringPolicy": {
                  "size": "10 MB"
               }
            },
            "DefaultRolloverStrategy": {
               "max": "10"
            }
         }
      },
      "loggers": {
         "logger": {
            "level": "info",
            "appender-ref": {
               "ref": "File"
            }
         },
         "root": {
            "level": "info",
            "AppenderRef": {
               "ref": "STDOUT"
            }
         }
      }
   }
}

我在spring控制台中遇到的错误:

2021-03-02 11:25:51,447 main ERROR Loggers cannot be configured without a name: arg[2](null)
2021-03-02 11:25:51,451 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.config.LoggerConfig for element logger: org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element logger are invalid org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element logger are invalid
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:280)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1002)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:942)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:934)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:552)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:288)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:618)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:691)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:708)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:263)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:203)
    at com.example.myapp.MyappApplication.main(MyappApplication.java:10)

2021-03-02 11:25:51,458 main ERROR Null object returned for logger in loggers.
2021-03-02 11:25:52,019 main ERROR Loggers cannot be configured without a name: arg[2](null)
2021-03-02 11:25:52,020 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.config.LoggerConfig for element logger: org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element logger are invalid org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element logger are invalid
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:280)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1002)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:942)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:934)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:552)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:288)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:618)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:691)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:708)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.reinitialize(Log4J2LoggingSystem.java:207)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:73)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.initialize(Log4J2LoggingSystem.java:163)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:312)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:362)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
    at com.example.myapp.MyappApplication.main(MyappApplication.java:10)

2021-03-02 11:25:52,021 main ERROR Null object returned for logger in loggers.

控制台输出很好,正如您所看到的,不起作用的是外部文件中的输出。

2021-03-02 11:25:52,197 [main] INFO  com.example.myapp.MyappApplication - No active profile set, falling back to default profiles: default
2021-03-02 11:25:53,153 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-03-02 11:25:53,249 [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 81 ms. Found 1 JPA repository interfaces.
2021-03-02 11:25:54,493 [main] INFO  org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
2021-03-02 11:25:54,507 [main] INFO  org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
hfsqlsce

hfsqlsce1#

看起来你可能有一个拼写错误-应该是 AppenderRef 而不是 appender-ref . 示例见手册-https://logging.apache.org/log4j/2.x/manual/configuration.html#configuration_with_json.

相关问题