弹簧启动记录石英不打印[使用log4j2]

时间:2017-09-26 06:54:06

标签: spring spring-boot

我使用Quartz和log4j2.properties进行Spring启动。与石英作业相关的日志没有打印出来。所有其他日志都打印出来。我应该添加任何特定的配置来打印吗?

public class Scheduler {
    private static Logger log = LogManager.getLogger(); 

    public static void main(String[] args) throws Exception {
    //following getting printed
        log.info("Scheduler is testing");
        SpringApplication.run(Scheduler.class, args);

    }

    @Scheduled(cron="*/2 * * * * *")
    public void execute() {
        //following not getting printed
        log.info("Scheduler  ....");
    }
}

来自cron的日志未打印。我正在使用log4j2.properties进行配置。我正在使用的属性:

#START
name=PropertiesConfig
#Folder location
property.filename = /Users/folder
#both console and file
appenders = console, file

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/gt.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

loggers=file
#Project package base
logger.file.name=com.mypackage
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
#END

1 个答案:

答案 0 :(得分:2)

确保刚刚调用了execute()方法。如果您使用的是@Scheduled,请确保您拥有@EnableScheduling注释,例如:

@SpringBootApplication
@EnableScheduling // Make sure this is present
public class ScheduledTaskApplication {
    private static final Logger log = LogManager.getLogger();

        public static void main(String[] args) {
            log.info("Scheduler is testing");
            SpringApplication.run(ScheduledTaskApplication.class, args);
    }

    @Scheduled(cron="*/2 * * * * *")
    public void execute() {
        //following not getting printed
        log.info("Scheduler  ....");
    }
}

此外,请确保您在logging.config中配置了application.properties属性:

logging.config=classpath:log4j2.properties

如果你不这样做,似乎日志记录表现得很奇怪,因为main()中的消息将写入正确的记录器,但之后它会将其更改为rootLogger,从而导致该类中的所有其他消息(不仅是test()中要写入控制台的消息)。