2015-10-04 67 views
0

我正在嘗試使用logback將一些行記錄到日誌文件中。該文件已正確創建,並且我的控制檯輸出實際上已寫入日誌文件中。爲什麼logback沒有用Spring Boot記錄某些行?

然後,我在我的代碼中插入了一些logger.debug()指令,我在日誌中找不到這些指令。爲什麼?

我使用Spring啓動:

@SpringBootApplication 
public class MyApplication { 

    private static final Logger logger = 
      LoggerFactory.getLogger(MyApplication.class); 

    public static void main(String[] args) { 
     SpringApplication.run(MyApplication.class, args); 
    } 

    @Bean 
    InitializingBean myInitializer(final IdentityService identityService) { 
     return new InitializingBean() { 
      public void afterPropertiesSet() throws Exception { 
       logger.debug("Preparing things..."); 
       if (someCondition) { 
        doSomething(); 
        logger.debug("Done something."); 
       } else { 
        doSomethingElse(); 
        logger.debug("Done something else."); 
       } 

      } 
     }; 
    } 
} 

application.properties包括logging.file=logs/mylog.log這個文件被創建。

的logback的配置是非常容易的,它應該是正確的,並且位置正確(如果我改變這個文件,例如,通過引入一個日誌文件名模式,它的工作原理):

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <include resource="org/springframework/boot/logging/logback/base.xml"/> 
    <logger name="org.springframework.web" level="DEBUG"/> 
</configuration> 

爲什麼我沒有看到我的日誌文件中的日誌說明?是因爲我正在構建一個InitializingBean嗎?我如何記錄這些行?

回答

1

由於base.xml文件具有在INFO級別設置的根日誌記錄。請試試這個,讓您的調試日誌: -

<?xml version="1.0" encoding="UTF-8"?>  
<included> 
    <include resource="org/springframework/boot/logging/logback/defaults.xml" /> 
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/> 
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" /> 
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> 
    <logger name="org.springframework.web" level="DEBUG"/> 
    <root level="DEBUG"> 
     <appender-ref ref="CONSOLE" /> 
     <appender-ref ref="FILE" /> 
    </root> 
</included> 
+0

謝謝,我試圖添加<根級別=「DEBUG」>,它工作正常,但它是一種太多記錄。有沒有辦法增加日誌記錄級別以僅爲我的類進行調試? – Manu

+1

好的,我只是在我的應用程序屬性中添加了logging.level.com.lh.batactiviti = DEBUG。在理解問題出現在base.xml後,這已經足夠了 – Manu

相關問題