带有Slf4的Log4j2和Spring Boot初始化很好,但不记录任何其他内容

时间:2016-12-05 21:54:48

标签: spring-boot slf4j log4j2

我最近搬到了Spring Boot,并使用了log4j2和Sl4j,它运行得非常好。在使用Feign,hystrix和Spring云库进行了大量更改后,某些时候log4j日志记录被破坏了。我的第一次尝试是删除我添加的那些奇特的春天云库,但不幸的是,这并没有解决这个问题。我在部署和初始化日志记录上下文时看到了一些奇怪的东西。以下是我的追踪。

   Initializing configuration XmlConfiguration[location=/com/config/my-service/log4j2.xml]
   2016-12-05 15:10:33,748 main DEBUG Installed script engines
   2016-12-05 15:10:34,403 main DEBUG Oracle Nashorn Version: 1.8.0_25, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
   2016-12-05 15:10:34,404 main DEBUG PluginManager 'Core' found 101 plugins
   2016-12-05 15:10:34,404 main DEBUG PluginManager 'Level' found 0 plugins
   2016-12-05 15:10:34,413 main DEBUG Starting 1 Log4j2Scheduled threads
   2016-12-05 15:10:34,418 main DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
   2016-12-05 15:10:34,456 main TRACE TypeConverterRegistry initializing.
   2016-12-05 15:10:34,457 main DEBUG PluginManager 'TypeConverter' found 23 plugins
   2016-12-05 15:10:34,472 main DEBUG createProperty(name="log-path", value="/com/logs")
   2016-12-05 15:10:34,473 main DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
   2016-12-05 15:10:34,474 main DEBUG createProperty(name="module", value="my-service")
   2016-12-05 15:10:34,474 main DEBUG Building Plugin[name=properties, class=org.apache.logging.log4j.core.config.PropertiesPlugin].
   2016-12-05 15:10:34,483 main DEBUG configureSubstitutor(={log-path=/com/logs, module=my-service}, Configuration(SERVICE))
   2016-12-05 15:10:34,484 main DEBUG PluginManager 'Lookup' found 14 plugins
   2016-12-05 15:10:34,486 main DEBUG Building Plugin[name=layout, class=com.mycompany.logging.jsonlayout.log4j2.MycompanyJsonLayout].
   2016-12-05 15:10:34,487 main DEBUG createLayout(pretty="true", layoutVersion="2", showOrigin="false")
   2016-12-05 15:10:34,894 main DEBUG Building Plugin[name=SizeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy].
   2016-12-05 15:10:34,897 main DEBUG createPolicy(size="100 MB")
   2016-12-05 15:10:34,899 main DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
   2016-12-05 15:10:34,900 main DEBUG createPolicy(={SizeBasedTriggeringPolicy(size=104857600)})
   2016-12-05 15:10:34,900 main DEBUG Building Plugin[name=DefaultRolloverStrategy, class=org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy].
   2016-12-05 15:10:34,902 main DEBUG createStrategy(max="1", min="null", fileIndex="null", compressionLevel="null", ={}, stopCustomActionsOnError="true", Configuration(SERVICE))
   2016-12-05 15:10:34,904 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender].
   2016-12-05 15:10:34,908 main DEBUG createAppender(fileName="/com/logs/my-service.json", filePattern="/com/logsmy-service.json.%i.gz", append="null", name="comjson", bufferedIO="null", bufferSize="null", immediateFlush="null", Policies(CompositeTriggeringPolicy(policies=[SizeBasedTriggeringPolicy(size=104857600)])), DefaultRolloverStrategy(DefaultRolloverStrategy(min=1, max=1)), MycompanyJsonLayout(com.mycompany.logging.jsonlayout.log4j2.MycompanyJsonLayout@41ee392b), Filter=null, ignoreExceptions="null", advertise="null", advertiseURI="null", Configuration(SERVICE))
   2016-12-05 15:10:34,914 main DEBUG Starting RollingFileManager /com/logs/my-service.json
   2016-12-05 15:10:34,914 main DEBUG PluginManager 'FileConverter' found 2 plugins
   2016-12-05 15:10:34,915 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
   2016-12-05 15:10:34,916 main DEBUG createAppenders(={comjson})
   2016-12-05 15:10:34,917 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
   2016-12-05 15:10:34,918 main DEBUG createAppenderRef(ref="comjson", level="null", Filter=null)
   2016-12-05 15:10:34,918 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
   2016-12-05 15:10:34,927 main DEBUG createLogger(additivity="false", level="DEBUG", name="com.mycompany.ecommerce", includeLocation="null", ={comjson}, ={}, Configuration(SERVICE), Filter=null)
   2016-12-05 15:10:34,931 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
   2016-12-05 15:10:34,933 main DEBUG createLogger(additivity="true", level="DEBUG", name="com.mycompany.service.interceptor", includeLocation="null", ={}, ={}, Configuration(SERVICE), Filter=null)
   2016-12-05 15:10:34,933 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
   2016-12-05 15:10:34,935 main DEBUG createLogger(additivity="true", level="DEBUG", name="com.com", includeLocation="null", ={}, ={}, Configuration(SERVICE), Filter=null)
   2016-12-05 15:10:34,936 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
   2016-12-05 15:10:34,937 main DEBUG createLogger(additivity="true", level="INFO", name="performance", includeLocation="null", ={}, ={}, Configuration(SERVICE), Filter=null)
   2016-12-05 15:10:34,938 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
   2016-12-05 15:10:34,939 main DEBUG createLogger(additivity="true", level="INFO", name="com.mycompany.ecommerce.health", includeLocation="null", ={}, ={}, Configuration(SERVICE), Filter=null)
   2016-12-05 15:10:34,939 main DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
   2016-12-05 15:10:34,940 main DEBUG createAppenderRef(ref="comjson", level="null", Filter=null)
   2016-12-05 15:10:34,941 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
   2016-12-05 15:10:34,942 main DEBUG createLogger(additivity="null", level="INFO", includeLocation="null", ={comjson}, ={}, Configuration(SERVICE), Filter=null)
   2016-12-05 15:10:34,942 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
   2016-12-05 15:10:34,944 main DEBUG createLoggers(={com.mycompany.ecommerce, com.mycompany.service.interceptor, com.com, performance, com.mycompany.ecommerce.health, root})
   2016-12-05 15:10:34,946 main DEBUG Configuration XmlConfiguration[location=/com/config/my-service/log4j2.xml] initialized
   2016-12-05 15:10:34,946 main DEBUG Starting configuration XmlConfiguration[location=/com/config/my-service/log4j2.xml]
   2016-12-05 15:10:34,948 main DEBUG Started configuration XmlConfiguration[location=/com/config/my-service/log4j2.xml] OK.
   2016-12-05 15:10:34,957 main TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@87aac27...
   2016-12-05 15:10:34,957 main TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
   2016-12-05 15:10:34,958 main TRACE DefaultConfiguration stopping root LoggerConfig.
   2016-12-05 15:10:34,958 main TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
   2016-12-05 15:10:34,958 main TRACE DefaultConfiguration stopping remaining Appenders.
   2016-12-05 15:10:34,959 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
   2016-12-05 15:10:34,959 main TRACE DefaultConfiguration stopped 1 remaining Appenders.
   2016-12-05 15:10:34,960 main TRACE DefaultConfiguration cleaning Appenders from 1 LoggerConfigs.
   2016-12-05 15:10:34,960 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@87aac27 OK
   2016-12-05 15:10:35,019 main TRACE Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@15761df8
   2016-12-05 15:10:35,020 main TRACE Reregistering context (1/1): '5b2133b1' org.apache.logging.log4j.core.LoggerContext@6ab7a896
   2016-12-05 15:10:35,023 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1'
   2016-12-05 15:10:35,023 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1,component=StatusLogger'
   2016-12-05 15:10:35,024 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1,component=ContextSelector'
   2016-12-05 15:10:35,024 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=*'
   2016-12-05 15:10:35,025 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1,component=Appenders,name=*'
   2016-12-05 15:10:35,025 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1,component=AsyncAppenders,name=*'
   2016-12-05 15:10:35,026 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1,component=AsyncLoggerRingBuffer'
   2016-12-05 15:10:35,026 main TRACE Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=*,subtype=RingBuffer'
   2016-12-05 15:10:35,029 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1
   2016-12-05 15:10:35,038 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=StatusLogger
   2016-12-05 15:10:35,040 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=ContextSelector
   2016-12-05 15:10:35,043 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=
   2016-12-05 15:10:35,044 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=performance
   2016-12-05 15:10:35,044 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=com.com
   2016-12-05 15:10:35,044 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=com.mycompany.ecommerce
   2016-12-05 15:10:35,045 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=com.mycompany.service.interceptor
   2016-12-05 15:10:35,045 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=Loggers,name=com.mycompany.ecommerce.health
   2016-12-05 15:10:35,047 main DEBUG Registering MBean org.apache.logging.log4j2:type=5b2133b1,component=Appenders,name=comjson
   2016-12-05 15:10:35,052 main TRACE Using default SystemClock for timestamps.
   2016-12-05 15:10:35,052 main TRACE Using DummyNanoClock for nanosecond timestamps.
   2016-12-05 15:10:35,053 main DEBUG Reconfiguration complete for context[name=5b2133b1] at URI /com/config/my-service/log4j2.xml (org.apache.logging.log4j.core.LoggerContext@6ab7a896) with optional ClassLoader: null
   2016-12-05 15:10:35,053 main DEBUG Shutdown hook enabled. Registering a new one.
   2016-12-05 15:10:35,054 main DEBUG LoggerContext[name=5b2133b1, org.apache.logging.log4j.core.LoggerContext@6ab7a896] started OK.
   2016-12-05 15:10:35,991 main DEBUG Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@35047d03

我对Unregistering mBeans很好奇。因为我的Appenders未注册。同样低于这个对我来说毫无意义!

    TRACE Stopping       org.apache.logging.log4j.core.config.DefaultConfiguration@87aac27...
  2016-12-05 15:10:34,957 main TRACE DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
  2016-12-05 15:10:34,958 main TRACE DefaultConfiguration stopping root LoggerConfig.
  2016-12-05 15:10:34,958 main TRACE DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
  2016-12-05 15:10:34,958 main TRACE DefaultConfiguration stopping remaining Appenders.
  2016-12-05 15:10:34,959 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
  2016-12-05 15:10:34,959 main TRACE DefaultConfiguration stopped 1 remaining Appenders.
  2016-12-05 15:10:34,960 main TRACE DefaultConfiguration cleaning Appenders from 1 LoggerConfigs.
  2016-12-05 15:10:34,960 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration

我从类路径中排除了不必要的日志记录相关库,如commons-logging,jul-to-slf4j,log4j-over-slf4j和logback stuff。

我的类路径中有以下jar文件

  1. jcl-over-slf4j:1.7.21
  2. jboss-logging:3.3.0.Final
  3. log4j-api:2.6.2
  4. log4j-core:2.6.2
  5. log4j-slf4j-impl:2.6.2
  6. log4j-web:2.4
  7. spring-boot-starter-log4j2:1.4.1.RELEASE
  8. 弹簧引导起动日志记录:1.4.1.RELEASE
  9. 我在my-service.json文件中看到的只有以下内容

    {
     "time" : "2016-12-05T21:22:08.742Z",
     "service" : { },
     "message" : {
       "category" : "org.eclipse.jetty.util.log",
       "severity" : "INFO",
       "text" : "Logging initialized @2227ms"
     },
     "v" : "2"
    }
    

    有人可以帮我解决这里发生的事吗?我也在控制台中看到了INFO日志。但是appender似乎没有起作用。

1 个答案:

答案 0 :(得分:0)

我使用的<-D -D选项是-Dlog4j.ConfigurationFile。其中一个罐子与弹簧靴配置发生冲突。使用spring boot的-Dlogging.config选项解决了这个问题。