项目GitHub地址 :
https://github.com/FrameReserve/TrainingBoot
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())); } }