1. 程式人生 > >關於logback.xml檔案 日誌輸出位置的問題

關於logback.xml檔案 日誌輸出位置的問題

<!-- ch.qos.logback.classic.filter.ThresholdFilter  臨界值過濾器, 過濾掉低於指定臨界值的日誌
ch.qos.logback.classic.filter.LevelFilter   將過濾器的日誌級別配置為INFO,所有INFO級別的日誌交給appender處理,非INFO級別的日誌,被過濾掉。 -->
<configuration>
	<property name="APP_Name" value="8080-springboot-pro" /> //這裡為此專案的日誌資料夾名
	<property name="log.dir" value="~/Desktop/files"></property> //這裡為日誌的儲存地址
	<timestamp key="bySecond" datePattern="yyyyMMdd HHmmss"/>
	<contextName>${APP_Name}</contextName>

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{85} [%file:%line] - %msg%n</Pattern>
		</layout>
	</appender>

	<!-- 按日期和大小區分的滾動日誌 -->
	<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<encoder>
			<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{85} - %msg%n</Pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.dir}/${APP_Name}/info/info.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<maxHistory>30</maxHistory>

			<timeBasedFileNamingAndTriggeringPolicy
					class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
	</appender>


	<!-- 按日期和大小區分的滾動日誌 -->
	<appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 必須指定,否則不會往檔案輸出內容 -->
		<encoder>
			<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{85} - %msg%n</Pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUG</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>

		<!-- 必需要指定rollingPolicy 與 triggeringPolicy 屬性   否則不會生成檔案-->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.dir}/${APP_Name}/debug/debug.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<maxHistory>30</maxHistory>

			<timeBasedFileNamingAndTriggeringPolicy
					class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>

		</rollingPolicy>
	</appender>


	<!-- error級別只按日期滾動生成日誌 -->
	<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- 必須指定,否則不會往檔案輸出內容 -->
		<encoder>
			<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{85} - %msg%n</Pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>ERROR</level>
			<!--	<onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>-->
		</filter>

		<!-- 必需要指定rollingPolicy 與 triggeringPolicy 屬性   否則不會生成檔案-->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.dir}/${APP_Name}/error/error.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<maxHistory>30</maxHistory>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<!-- 預設值是10MB。 -->
		<!--     <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                  <maxFileSize>5MB</maxFileSize>
            </triggeringPolicy>  -->
	</appender>

	<!-- 滾動記錄檔案 -->
	<appender name="MONITOR" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<encoder>
			<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{85} - %msg%n</Pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>DEBUG</level>
		</filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.dir}/${APP_Name}/monitor/monitor.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
			<maxHistory>30</maxHistory>
			<timeBasedFileNamingAndTriggeringPolicy
					class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
	</appender>

	<logger name="org" level="INFO" />  <!--將org包下面的所有日誌級別設為了ERROR -->
	<logger name="monitor" additivity="false" level="DEBUG" />

	<logger name="monitor" additivity="false" level="DEBUG">
		<appender-ref ref="MONITOR" />
	</logger>

	<root level="DEBUG">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE_INFO" />
		<appender-ref ref="FILE_DEBUG" /> //上線時 這個需註釋掉,debug級別的日誌
		<appender-ref ref="FILE_ERROR" />
	</root>
</configuration>