嗨,我正在使用logback版本1.1.10,我的配置如下: 我在记录事件时遇到了一个大问题,无法找到所需的日志。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="abc_req" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log}/abc/request/request.log</File>
<encoder>
<pattern>%m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log}/abc/request/request.log.%d{yyyy-MM-dd-HH}</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="abc1_res" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log}/abc1/response/response.log</File>
<encoder>
<pattern>%m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log}/abc1/response/response.log.%d{yyyy-MM-dd-HH}</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="abc2_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${log}/abc2/error/error.log</File>
<encoder>
<pattern>%m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log}/abc2/error/error.log.%d{yyyy-MM-dd-HH}</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p:: %m%n</pattern>
</encoder>
</appender>
<logger name="abcLogRequest" level="INFO" additivity="false">
<appender-ref ref="abc_req"/>
</logger>
<logger name="abc1LogResponse" level="INFO" additivity="false">
<appender-ref ref="abc1_res"/>
</logger>
<logger name="abc2LogError" level="ERROR" additivity="false">
<appender-ref ref="abc2_error"/>
</logger>
<appender name="requestQueue" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>1000</queueSize>
<appender-ref ref="abc_req"/>
</appender>
<appender name="responseQueue" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>1000</queueSize>
<appender-ref ref="abc1_res"/>
</appender>
<appender name="errorQueue" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>500</queueSize>
<appender-ref ref="abc2_error"/>
</appender>
<!--
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/> -->
<root level="INFO">
<appender-ref ref="stdout"/>
</root>
</configuration>
但是在签入滚动文件时,文件中缺少很多日志条目。
在上述情况下确实需要帮助。
任何帮助/建议真的可以帮助我很多!
终于解决了!
问题:有 2 个 JVM 尝试写入一个公共文件。
所以只需在滚动文件追加器中添加以下内容
<prudent>true</prudent>
和删除配置中的文件参数。
<appender name="abc_req" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<encoder>
<pattern>%m%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log}/abc/request/request.log.%d{yyyy-MM-dd-HH}</fileNamePattern>
</rollingPolicy>
</appender>
同样通过滚动策略中的文件名模式,我能够给出正确的文件路径。