使用springboot集成logback配置

DBC 391 0

先加一个配置文件

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true">

	<springProperty scope="context" name="LOG_LEVEL" source="logging.level.root"/>
	<springProperty scope="context" name="APPNAME" source="logging.name"/>
	<property name="LOG_HOME" value="/data/weblog/xdclass" />

	<jmxConfigurator />

	<appender name="error_file"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_HOME}/${APPNAME}/web_error.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/${APPNAME}/web_error_%d{yyyy-MM-dd}_%i.log.zip
			</fileNamePattern>
			<maxHistory>7</maxHistory>
			<cleanHistoryOnStart>true</cleanHistoryOnStart>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>

		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>ERROR</level>
		</filter>
		<encoder>
			<charset>UTF-8</charset>
			<pattern>%date [%thread] [%file:%line] [%level %logger{0}] - %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="warn_file"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_HOME}/${APPNAME}/web_warn.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/${APPNAME}/web_warn_%d{yyyy-MM-dd}_%i.log.zip
			</fileNamePattern>
			<maxHistory>7</maxHistory>
			<cleanHistoryOnStart>true</cleanHistoryOnStart>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>

		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>WARN</level>
		</filter>
		<encoder>
			<charset>UTF-8</charset>
			<pattern>%date [%thread] [%file:%line] [%level %logger{0}] - %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="info_file"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_HOME}/${APPNAME}/web_info.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME}/${APPNAME}/web_info_%d{yyyy-MM-dd}_%i.log.zip
			</fileNamePattern>
			<maxHistory>7</maxHistory>
			<cleanHistoryOnStart>true</cleanHistoryOnStart>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>100MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>

		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>${LOG_LEVEL}</level>
		</filter>
		<encoder>
			<charset>UTF-8</charset>
			<pattern>%date [%thread] [%file:%line] [%level %logger{0}] - %msg%n
			</pattern>
		</encoder>
	</appender>


	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<charset>UTF-8</charset>
			<pattern>%date [%thread] [%level %logger{0}:%line] - %msg%n
			</pattern>
		</encoder>
	</appender>

	<!--异步输出-->
	<appender name="async_info_log" class="ch.qos.logback.classic.AsyncAppender">
		<discardingThreshold>0</discardingThreshold>
		<queueSize>512</queueSize>
		<includeCallerData>true</includeCallerData>
		<appender-ref ref="info_file"/>
	</appender>

	<root level="${LOG_LEVEL}">
		<appender-ref ref="async_info_log" />
		<appender-ref ref="warn_file" />
		<appender-ref ref="error_file" />
		<appender-ref ref="STDOUT" />
	</root>
</configuration>

简单使用

import com.xdclass.couponapp.service.CouponService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class UpdateCouponJob {

    private static final Logger logger = LoggerFactory.getLogger(UpdateCouponJob.class);
    @Resource
    private CouponService couponService;

    @Scheduled(cron = "0/5 * * * * ?")
    public void updateCoupon(){
        System.out.println("enter update coupon job");
        logger.info("enter update coupon job");
         couponService.updateCouponMap();
     }

}

使用springboot集成logback配置插图

温馨提示

可以看到打印的类,以及各种信息,比如第几行都给你打印出来,这里只是说一种简单的使用!

发表评论 取消回复
表情 图片 链接 代码

分享