我需要选择一个日志框架来替换log4j。 我最初选择了slf4j + logback进行日志记录,并编写了以下配置,它将应用程序日志和apache cxf的日志输出到单独的文件中,并在控制台上输出spring / hibernate日志:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>.%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %n
</Pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
</appender>
<appender name="dailyRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>D:\\MYDIR\\logs\\app_logback.%d{yyyy-MM-dd}.log</FileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
</encoder>
</appender>
<appender name="minuteRollingFileAppenderCxf" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>D:\\MYDIR\\logs\\app_logback_cxf.%d{yyyy-MM-dd_HH-mm}.log</FileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
</encoder>
</appender>
<logger name="com.mycustomcode" additivity="false">
<level value="DEBUG" />
<appender-ref ref="dailyRollingFileAppender"/>
</logger>
<logger name="org.apache" additivity="false">
<level value="INFO" />
<appender-ref ref="minuteRollingFileAppenderCxf"/>
</logger>
<root>
<level value="INFO" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
但是,我开始了解log4j2中的异步日志记录,我想在我的设置中使用log4j2实现异步日志记录,我编写了以下配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RandomAccessFile name="RandomAccessFile" fileName="D:\\MYDIR\\logs\\app_log4j2.log" immediateFlush="false" append="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
<RandomAccessFile name="RandomAccessFile2" fileName="D:\\MYDIR\\logs\\app_log4j2_cxf.log" immediateFlush="false" append="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</RandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.mycustomcode" level="debug" includeLocation="true" additivity="false">
<AppenderRef ref="RandomAccessFile"/>
</AsyncLogger>
<AsyncLogger name="org.apache" level="debug" includeLocation="true" additivity="false">
<AppenderRef ref="RandomAccessFile2"/>
</AsyncLogger>
<Root level="debug" includeLocation="false">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
log4j2配置有效,但它只在其文件(app_log4j2.log)中正确打印“com.mycustomcode”的日志。在app_log4j2_cxf.log文件中没有为“org.apache”打印任何内容 - 事实上,它是空白的,其日志将打印到服务器控制台。
有人可以指出我做错了什么吗? 感谢
答案 0 :(得分:0)
logback也有异步支持。检查AsyncAppender
答案 1 :(得分:0)
使用此配置的代码是否使用名称以“org.apache”开头的Logger?如果没有,您将看不到此记录器的任何输出。
要验证这一点,请使用以下配置:
Logger logger = LogManager.getLogger("org.apache.anything");
logger.debug("test");