我真的需要一些帮助来处理log4j的日志输出。我以前从未使用过log4j,并且正在继承此代码。调用API后,日志将同时输出到控制台和日志文件。日志文件是正确的,但是控制台首先以json格式(需要!),然后以纯文本格式在多行中获取了错误(不需要!)。
我已经阅读了S.O上的其他文章,并且我认为,这与记录器除了附加器之外还发送默认日志有关。大多数建议都说将可加性设置为false等,但是我已经掌握了一切,但仍然似乎无法删除第二个不需要的纯文本错误输出。
欢迎任何建议!
<property name="FILE_PATH" value="/usr/local/tomcat/logs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp/>
<version/>
<pattern>
<pattern>
{"date": "%date{dd MMM yyyy HH:mm:ss.SSS XX}", "class": "%c", "level": "%p", "thread": "%t", "message": "%m", "map": "%mdc"}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${FILE_PATH}/enrollment.log</file>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%date{dd MMM yyyy HH:mm:ss.SSS XX}] %c %p [%t] %m {%mdc}%n</Pattern>
</layout>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp/>
<version/>
<pattern>
<pattern>
{"date": "%date{dd MMM yyyy HH:mm:ss.SSS XX}", "class": "%c", "level": "%p", "thread": "%t", "message": "%m", "map": "%mdc"}
</pattern>
</pattern>
</providers>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${FILE_PATH}/archived/enrollment.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- Send logs to both console and file audit -->
<logger name="com.package" level="info" additivity="false">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</logger>
<root level="info">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</root>