我一直在尝试使用webflux配置spring-boot 2.1以将访问日志存储在JSON中。此外,我需要将诸如协议,状态代码之类的信息作为独立的JSON字段(而不是消息的一部分)。通过互联网,我发现logstash-logback-encoder。似乎有我需要的一切。但是在运行时,出现以下错误:
ERROR in ch.qos.logback.core.FileAppender[accessLog] - Appender [accessLog] failed to append. java.lang.ClassCastException: ch.qos.logback.classic.spi.LoggingEvent cannot be cast to ch.qos.logback.access.spi.IAccessEvent
at java.lang.ClassCastException: ch.qos.logback.classic.spi.LoggingEvent cannot be cast to ch.qos.logback.access.spi.IAccessEvent
at at net.logstash.logback.composite.accessevent.AccessEventFormattedTimestampJsonProvider.getTimestampAsMillis(AccessEventFormattedTimestampJsonProvider.java:20)
at at net.logstash.logback.composite.FormattedTimestampJsonProvider.writeTo(FormattedTimestampJsonProvider.java:149)
at at net.logstash.logback.composite.JsonProviders.writeTo(JsonProviders.java:77)
at at net.logstash.logback.composite.CompositeJsonFormatter.writeEventToGenerator(CompositeJsonFormatter.java:189)
at at net.logstash.logback.composite.CompositeJsonFormatter.writeEventToOutputStream(CompositeJsonFormatter.java:166)
at at net.logstash.logback.encoder.CompositeJsonEncoder.encode(CompositeJsonEncoder.java:122)
at at net.logstash.logback.encoder.CompositeJsonEncoder.encode(CompositeJsonEncoder.java:34)
at at ch.qos.logback.core.OutputStreamAppender.subAppend(OutputStreamAppender.java:230)
at at ch.qos.logback.core.OutputStreamAppender.append(OutputStreamAppender.java:102)
at at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:84)
at at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:51)
at at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:270)
at at ch.qos.logback.classic.Logger.callAppenders(Logger.java:257)
at at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:421)
at at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
at at ch.qos.logback.classic.Logger.info(Logger.java:591)
at at reactor.util.Loggers$Slf4JLogger.info(Loggers.java:255)
at at reactor.netty.http.server.AccessLog.log(AccessLog.java:104)
at at reactor.netty.http.server.AccessLogHandler.lambda$write$0(AccessLogHandler.java:77)
at at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:511)
at at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:504)
at at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:483)
at at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:424)
at at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:103)
at at io.netty.util.internal.PromiseNotificationUtil.trySuccess(PromiseNotificationUtil.java:48)
at at io.netty.channel.ChannelOutboundBuffer.safeSuccess(ChannelOutboundBuffer.java:696)
at at io.netty.channel.ChannelOutboundBuffer.remove(ChannelOutboundBuffer.java:258)
at at io.netty.channel.nio.AbstractNioByteChannel.doWriteInternal(AbstractNioByteChannel.java:216)
at at io.netty.channel.nio.AbstractNioByteChannel.doWrite0(AbstractNioByteChannel.java:209)
at at io.netty.channel.socket.nio.NioSocketChannel.doWrite(NioSocketChannel.java:397)
at at io.netty.channel.AbstractChannel$AbstractUnsafe.flush0(AbstractChannel.java:934)
at at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.flush0(AbstractNioChannel.java:360)
at at io.netty.channel.AbstractChannel$AbstractUnsafe.flush(AbstractChannel.java:901)
at at io.netty.channel.DefaultChannelPipeline$HeadContext.flush(DefaultChannelPipeline.java:1396)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:776)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:768)
at at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:749)
at at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.flush(CombinedChannelDuplexHandler.java:533)
at at io.netty.channel.ChannelOutboundHandlerAdapter.flush(ChannelOutboundHandlerAdapter.java:115)
at at io.netty.channel.CombinedChannelDuplexHandler.flush(CombinedChannelDuplexHandler.java:358)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:776)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:768)
at at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:749)
at at io.netty.channel.ChannelDuplexHandler.flush(ChannelDuplexHandler.java:117)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:776)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:768)
at at io.netty.channel.AbstractChannelHandlerContext.flush(AbstractChannelHandlerContext.java:749)
at at io.netty.channel.ChannelDuplexHandler.flush(ChannelDuplexHandler.java:117)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:776)
at at io.netty.channel.AbstractChannelHandlerContext.invokeFlush(AbstractChannelHandlerContext.java:768)
我的配置非常简单:
<appender name="accessLog" class="ch.qos.logback.core.FileAppender">
<file>${LOGS}/access_log.log</file>
<encoder class="net.logstash.logback.encoder.LogstashAccessEncoder"/>
</appender>
<logger name="reactor.netty.http.server.AccessLog" level="DEBUG" additivity="false">
<appender-ref ref="accessLog"/>
</logger>
这时我被卡住了。我一直在搜索Internet并查看登录代码,但我仍然不知道要怎么做才能使AccessEvent包含更多信息,而不是LoggingEvent
答案 0 :(得分:1)
logstash-logback-encoder中的LogstashAccessEncoder
需要logback-access,它提供了AccessEvents。 logback-access仅适用于码头和tomcat。它不适用于反应堆网络。
Reactor-netty的reactor.netty.http.server.AccessLog
仅使用标准的日志事件(而不是AccessEvent)。因此,如果要使用logstash-logback-encoder,可以使用LogstashEncoder
而不是LogstashAccessEncoder
,如下所示:
<appender name="accessLog" class="ch.qos.logback.core.FileAppender">
<file>${LOGS}/access_log.log</file>
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<logger name="reactor.netty.http.server.AccessLog" level="DEBUG" additivity="false">
<appender-ref ref="accessLog"/>
</logger>
缺点是LogstashEncoder
对HTTP请求一无所知。它只知道通过Logger
记录的日志事件。因此,您无法像使用LogstashEncoder
一样配置使用LogstashAccessEncoder
时记录哪些HTTP详细信息。相反,您的日志事件将仅显示reactor.netty.http.server.AccessLog
提供的详细信息作为日志消息的一部分。这些详细信息将无法在JSON输出中作为单独的字段使用。尽管由于logstash-logback-encoder可以高度自定义,但是您可以编写一个custom JsonProvider
来解析reactor.netty.http.server.AccessLog
中的日志消息,并将其拆分为单独的JSON字段。