Logback配置指定log文件日志

前提:在项目中已配置好logback的情况下使用,此处以springboot项目环境为例。

1、定义常量

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

2、记录日志

private static final Logger logger = LoggerFactory.getLogger(Constants.LOGGER_LOAD_NET_ADS_REQUEST);
logger.info(“记录你要记录的日志”);

3、配置logback-spring.xml

<?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>
01-09 20:53