21 7月 2009

logback: 將各個log file依日期存放在同一目錄

偶爾會有這樣的需求,希望同一天的Log都在同一個目錄下,這時只要將TimeBasedRollingPolicy的FileNamePattern用
%d{yyyy/MM/dd/}/這類的Pattern就可以達成囉。

請見以下例子
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
	<appender name="SYSTEM" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<Prudent>true</Prudent>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<FileNamePattern>%d{yyyy/MM/dd/}/System.log</FileNamePattern>
		</rollingPolicy>
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>%d [%thread] %-5level %logger{35} - %msg%n
			</Pattern>
		</layout>
	</appender>
	<appender name="MAIL" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<Prudent>true</Prudent>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<FileNamePattern>%d{yyyy/MM/dd/}/Mail.log</FileNamePattern>
		</rollingPolicy>
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>%d [%thread] %-5level %logger{35} - %msg%n
			</Pattern>
		</layout>
	</appender>
	<root level="INFO">
		<appender-ref ref="SYSTEM" />
		<appender-ref ref="MAIL" />
	</root>
</configuration>

沒有留言: