如何为Log4j2中的Spring Boot启动日志定义单独的附加程序?

时间:2019-05-30 17:30:45

标签: spring-boot logging log4j2

当log4j2与SpringBoot-2集成时,出现异常。当我尝试了解异常时,似乎log4j2的JsonLayout试图将SpringBoot的AutoConfigurationReport的日志行转换为JSON。而且它失败了。

  

com.fasterxml.jackson.databind.exc.InvalidDefinitionException错误:   找不到类别的序列化器   org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportMessage   并没有发现创建BeanSerializer的属性(避免   异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过   参考链:   org.apache.logging.log4j.core.impl.Log4jLogEvent [“ message”])   com.fasterxml.jackson.databind.exc.InvalidDefinitionException:否   找到用于类的序列化器   org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportMessage   并没有发现创建BeanSerializer的属性(避免   异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过   参考链:   org.apache.logging.log4j.core.impl.Log4jLogEvent [“ message”])在   com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)     在   com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)     在   com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313)     在   com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)     在   com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)     在   com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)     在   com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)     在   com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2655)     在   com.fasterxml.jackson.core.base.GeneratorBase.writeObject(GeneratorBase.java:381)     在   org.apache.logging.log4j.core.jackson.ObjectMessageSerializer.serialize(ObjectMessageSerializer.java:44)     在   org.apache.logging.log4j.core.jackson.ObjectMessageSerializer.serialize(ObjectMessageSerializer.java:33)     在   com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)     在   com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.serializeAsField(SimpleBeanPropertyFilter.java:208)     在   com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFieldsFiltered(BeanSerializerBase.java:771)     在   com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:153)     在   com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)     在   com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)     在   com.fasterxml.jackson.databind.ObjectWriter $ Prefetch.serialize(ObjectWriter.java:1396)     在   com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1120)     在   com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:966)     在   org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:304)     在   org.apache.logging.log4j.core.layout.JsonLayout.toSerializable(JsonLayout.java:291)     在   org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:255)     在   org.apache.logging.log4j.core.layout.JsonLayout.toSerializable(JsonLayout.java:68)     在   org.apache.logging.log4j.core.layout.AbstractJacksonLayout.toSerializable(AbstractJacksonLayout.java:44)     在   org.apache.logging.log4j.core.layout.AbstractStringLayout.toByteArray(AbstractStringLayout.java:304)     在   org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:210)     在   org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:37)     在   org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:197)     在   org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:190)     在   org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:181)     在   org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)     在   org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)     在   org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)     在   org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)     在   org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)     在   org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)     在   org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)     在   org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)     在   org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)     在org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)     在   org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)     在   org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)     在   org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)     在   org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1985)     在   org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1843)     在   org.apache.commons.logging.LogAdapter $ Log4jLog.log(LogAdapter.java:265)     在   org.apache.commons.logging.LogAdapter $ Log4jLog.debug(LogAdapter.java:235)     在   org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener.logAutoConfigurationReport(ConditionEvaluationReportLoggingListener.java:132)     在   org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener.logAutoConfigurationReport(ConditionEvaluationReportLoggingListener.java:108)     在   org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener.onApplicationEvent(ConditionEvaluationReportLoggingListener.java:97)     在   org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener $ ConditionEvaluationReportListener.onApplicationEvent(ConditionEvaluationReportLoggingListener.java:173)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)     在   org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402)     在   org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359)     在   org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:896)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552)     在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)     在   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:316)     在   org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139)     在   org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:203)     在   org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:114)     在   org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:71)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)     在   org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75)     在   org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)     在   org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:347)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:306)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)

我正在使用的log4j2.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" name="someName" packages="com.somepackage.somename">
   <Appenders>
      <Console name="consoleAppender" target="SYSTEM_OUT">
         <Filters>
            <ThresholdFilter level="Debug" onMatch="ACCEPT" />
         </Filters>
         <JSONLayout complete="false" compact="true" eventEol="true" objectMessageAsJsonObject="true" includeStacktrace="true" stacktraceAsString="true" />
      </Console>
   </Appenders>
   <Loggers>
      <Root level="Debug">
         <AppenderRef ref="consoleAppender" />
      </Root>
   </Loggers>
</Configuration>

0 个答案:

没有答案