我希望能够做到:
// A logger.
private static final Logger log = LoggerFactory.getLogger(Foo.class);
// Stats logger.
private static final Logger stats = LoggerFactory.getLogger("Stats");
每当我登录到stats
( stats.info(...)
) 时,我都希望日志条目转到一个名为Stats.log的文件。登录到log
应该正常。
我(可能是错误地)得到了以下内容Logback.xml
:
A failed config that adds nothing to the question now ... removed.
我似乎无法找到任何告诉我如何根据记录器名称选择附加程序的信息。根据日志级别有很多选择。
添加
这是我最终得到的配置:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>Log.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>Log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 1MB -->
<maxFileSize>1MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- Check on startup too. -->
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="STATS" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>Stats.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>Stats-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 1MB -->
<maxFileSize>1MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- Check on startup too. -->
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%date %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%level %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
<logger name="Stats" level="info">
<appender-ref ref="STATS" />
</logger>
</configuration>
它工作正常 - 尽管统计信息也记录到普通日志文件中,我可能需要使用过滤器将其删除。