2017-09-26 80 views
1

我使用Quartz的Spring引導以及log4j2.properties。與石英作業相關的日誌沒有被打印。所有其他日誌正在打印。我應該添加任何特定的配置來獲取此打印?從石英彈簧引導日誌不打印[使用log4j2]

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

你確定你的'execute'方法被執行了嗎?你有沒有嘗試在它內部放置一個斷點並用你的IDE進行調試? – pleft

+0

是的。它在控制檯上打印,而不是在日誌文件中打印。 – TomJava

回答

1

確保只調用​​方法。如果您使用@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 

如果你不」 t做到這一點,因爲main()中的消息將被寫入適當的記錄器,但之後它將其更改爲rootLogger,導致該類中的所有其他消息(不僅是test()內的消息寫入控制檯)。

+0

這也存在。日誌進入控制檯,不會寫入文件。 – TomJava

+0

@TomJava你可以添加你的日誌配置到你的問題嗎? – g00glen00b

+0

添加到問題中。 – TomJava