对于以下 logback.xml,不会创建日志文件 foo.log。
<appender name="example" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${user.home}/log/main.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>50</maxHistory>
<totalSizeCap>300MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="example.foo" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${user.home}/log/foo.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>50</maxHistory>
<totalSizeCap>300MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="example" level="DEBUG" additivity="false">
<appender-ref ref="example"/>
<appender-ref ref="console"/>
</logger>
<logger name="example.foo" level="DEBUG" additivity="false">
<appender-ref ref="example.foo"/>
<appender-ref ref="console"/>
</logger>
<root level="DEBUG">
<appender-ref ref="example"/>
<appender-ref ref="console"/>
</root>
</configuration>
爪哇代码:
Logger logger = LoggerFactory.getLogger("example.foo");
logger.debug("hello foo");
日志消息显示在控制台上,但未创建日志文件 foo.log。
Logger logger = LoggerFactory.getLogger("example");
logger.debug("hello example");
此消息显示在控制台和日志文件 main.log 上。