Logback配置指定log文件日誌

Logback配置指定log文件日誌

前提:在項目中已配置好logback的狀況下使用,此處以springboot項目環境爲例。

一、 定義常量java

Constants.java中定義
String LOGGER_LOAD_NET_ADS_REQUEST = "load_net_ads";

二、 記錄日誌web

private static final Logger logger = LoggerFactory.getLogger(Constants.LOGGER_LOAD_NET_ADS_REQUEST);
logger.info(「記錄你要記錄的日誌」);

三、配置logback-spring.xmlspring

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">
	<!--<include resource="org/springframework/boot/logging/logback/base.xml"/>-->
	<include resource="org/springframework/boot/logging/logback/defaults.xml" />
	<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

	<logger name="ch.qos.logback" level="WARN"/>
	<logger name="org.apache" level="WARN"/>
	<logger name="org.apache.catalina.startup.DigesterFactory" level="OFF"/>
	<logger name="org.hibernate.validator" level="WARN"/>
	<logger name="org.hibernate" level="WARN"/>
	<logger name="org.hibernate.ejb.HibernatePersistence" level="OFF"/>
	<logger name="org.springframework" level="WARN"/>
	<logger name="org.springframework.web" level="WARN"/>
	<logger name="org.springframework.security" level="WARN"/>
	<logger name="org.springframework.cache" level="WARN"/>
	<logger name="org.xnio" level="WARN"/>
	<logger name="sun.rmi" level="WARN"/>
	<logger name="org.eclipse.jetty" level="INFO"/>

	<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
		<resetJUL>true</resetJUL>
	</contextListener>

    <!--source屬性是定義在application.xml下,此處用於轉化爲logback可識別的路徑-->
	<springProperty scope="context" name="workspace" source="joker.workspace"/>
	<springProperty scope="context" name="log-path" source="joker.log.path"/>
	<springProperty scope="context" name="base-package" source="joker.log.base-package" defaultValue="com.jooker"/>
	<springProperty scope="context" name="log-pattern" source="joker.log.service-pattern" defaultValue="%d %-5level [%thread] %logger{0}: %msg%n"/>

    <!--開發環境-->
	<springProfile name="dev">
		<logger name="${base-package}" level="DEBUG" />
		<logger name="org.springframework.web" level="INFO"/>
		<logger name="org.springframework.security" level="INFO"/>
		<logger name="org.springframework.cache" level="INFO"/>
		<logger name="druid.sql.Statement" level="INFO"/>
		<logger name="druid.sql.ResultSet" level="INFO"/>
		<logger name="druid.sql.Connection" level="INFO"/>
		<logger name="c.a.druid.pool.PreparedStatementPool" level="INFO"/>
		<root level="DEBUG">
			<appender-ref ref="CONSOLE" />
		</root>
	</springProfile>

    <!--測試環境&生產環境-->
	<springProfile name="test,prod">
		<property name="FILENAME_PATTERN_INFO" value="${log-path}/${workspace}.%d{yyyy-MM-dd}.info.log"/>
		<property name="FILENAME_PATTERN_WARN" value="${log-path}/${workspace}.%d{yyyy-MM-dd}.warn.log"/>
		<property name="FILENAME_PATTERN_ERROR" value="${log-path}/${workspace}.%d{yyyy-MM-dd}.error.log"/>
		<!-- load net ads -->
		<property name="FILENAME_PATTERN_LOAD_NET_ADS_REQUEST" value="${log-path}/${workspace}.%d{yyyy-MM-dd}.load-net-ads.log"/>

		<!-- INFO -->
		<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<fileNamePattern>${FILENAME_PATTERN_INFO}</fileNamePattern>
				<maxHistory>3</maxHistory>
			</rollingPolicy>
			<encoder>
				<charset>utf-8</charset>
				<Pattern>${log-pattern}</Pattern>
			</encoder>
			<filter class="ch.qos.logback.classic.filter.LevelFilter">
				<level>INFO</level>
				<onMatch>NEUTRAL</onMatch>
				<onMismatch>DENY</onMismatch>
			</filter>
            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
                <evaluator>
                    <expression>
                    <!-- load_net_ads 這個日誌不會在info日誌文件中記錄 -->
                    return logger.equals("load_net_ads");
                    </expression>
                </evaluator>
                <onMatch>DENY</onMatch>
                <onMismatch>ACCEPT</onMismatch>
            </filter>
		</appender>
		<appender name="ASYNC_INFO" class="ch.qos.logback.classic.AsyncAppender">
			<appender-ref ref="FILE_INFO"/>
		</appender>

		<!-- WARN -->
		<appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<fileNamePattern>${FILENAME_PATTERN_WARN}</fileNamePattern>
				<maxHistory>3</maxHistory>
			</rollingPolicy>
			<encoder>
				<charset>utf-8</charset>
				<Pattern>${log-pattern}</Pattern>
			</encoder>
			<filter class="ch.qos.logback.classic.filter.LevelFilter">
				<level>WARN</level>
				<onMatch>ACCEPT</onMatch>
				<onMismatch>DENY</onMismatch>
			</filter>
		</appender>
		<appender name="ASYNC_WARN" class="ch.qos.logback.classic.AsyncAppender">
			<appender-ref ref="FILE_WARN"/>
		</appender>

		<!-- ERROR -->
		<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<fileNamePattern>${FILENAME_PATTERN_ERROR}</fileNamePattern>
				<maxHistory>3</maxHistory>
			</rollingPolicy>
			<encoder>
				<charset>utf-8</charset>
				<Pattern>${log-pattern}</Pattern>
			</encoder>
			<filter class="ch.qos.logback.classic.filter.LevelFilter">
				<level>ERROR</level>
				<onMatch>ACCEPT</onMatch>
				<onMismatch>DENY</onMismatch>
			</filter>
		</appender>
		<appender name="ASYNC_ERROR" class="ch.qos.logback.classic.AsyncAppender">
			<appender-ref ref="FILE_ERROR"/>
		</appender>

		<!-- load net ads request -->
		<appender name="FILE_LOAD_NET_ADS_REQUEST" class="ch.qos.logback.core.rolling.RollingFileAppender">
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<fileNamePattern>${FILENAME_PATTERN_LOAD_NET_ADS_REQUEST}</fileNamePattern>
				<maxHistory>1</maxHistory>
			</rollingPolicy>
			<encoder>
				<charset>utf-8</charset>
				<Pattern>%msg%n</Pattern>
			</encoder>
			<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
				<evaluator>
					<expression>return logger.equals("load_net_ads");</expression>
				</evaluator>
				<onMatch>ACCEPT</onMatch>
				<onMismatch>DENY</onMismatch>
			</filter>
		</appender>

		<appender name="ASYNC_LOAD_NET_ADS_REQUEST" class="ch.qos.logback.classic.AsyncAppender">
			<queueSize>512</queueSize>
			<appender-ref ref="FILE_LOAD_NET_ADS_REQUEST"/>
			<discardingThreshold>0</discardingThreshold>
		</appender>

		<logger name="${base-package}" level="INFO" />
		<root level="INFO">
			<springProfile name="test">
				<appender-ref ref="CONSOLE" />
			</springProfile>
			<appender-ref ref ="ASYNC_INFO"/>
			<appender-ref ref ="ASYNC_WARN"/>
			<appender-ref ref ="ASYNC_ERROR"/>

			<!-- load net ads request -->
			<appender-ref ref ="ASYNC_LOAD_NET_ADS_REQUEST"/>
		</root>
	</springProfile>
</configuration>
相關文章
相關標籤/搜索