原创

Spring Boot(九)log扩展logback.xml级别


项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot (八)增加@Scheduled创建定时任务,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.9



Spring Boot配置文件:

src/main/resources/application.yml

#指定log的配置文件,以及记录Spring Boot的log级别  
logging:
    config: classpath:logbak.xml


Logbak配置文件:

支持控制台数据,文件输出。

src/main/resources/logbak.xml

<configuration>   
   
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">   
    <!-- encoder 默认配置为PatternLayoutEncoder -->   
    <encoder>   
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>   
    </encoder>   
  </appender>
  
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>../logs/TrainingBoot.log</file>
        <append>true</append>
        <encoder>
          <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
  </appender>    
   
  <root level="INFO">             
    <appender-ref ref="STDOUT" />   
  </root>     
     
 </configuration>


增加定时任务测试类:

package com.training.task.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.training.task.service.DemoTaskService;

@Service
public class DemoTaskServiceImpl implements DemoTaskService {

	private Logger logger = Logger.getLogger(DemoTaskServiceImpl.class);
	
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
    @Scheduled(cron="0/5 * *  * * ? ")
    public void getTaskLog4j() {
    	logger.info("Log4j,当前时间:" + dateFormat.format(new Date()));
    }
    
}




















正文到此结束
Loading...