我試圖將Spring
和Maven
添加到我的一個現有項目中,並且我發現無論如何配置,日誌似乎都超出了我的控制範圍。Log4j似乎不能在Spring Boot中工作
我試着把log4j.properties
放在src/main/java
和src/main/resources
(其實我不確定放在哪裏)。
但是,當我使用Log4j
進行日誌記錄時,日誌僅顯示在控制檯中,但我將其配置爲文件。
我log4j.properties
是這樣的:
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.encoding=utf-8
log4j.appender.A1.File=E:\Programminglog\debug.log
log4j.appender.A1.Threshold = DEBUG
log4j.appender.A1.Append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
我不知道如果我錯過了什麼或Spring
覆蓋一些設置,因爲我是新來Maven
和Spring
。
PS:之前我在pom
的.xml加Log4j
依賴性,雖然沒有我用org.apache.log4j.Logger
這是我application.java看起來如何編譯錯誤,如:
@Configuration
@EnableAutoConfiguration
@ComponentScan({"hello","wodinow.weixin.jaskey"})
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
LogUtil.info("Application Boots!");// here this line does not show in the file
}
@Bean
public CommandService commandService(){
return CommandService.getInstance();
}
}
你在運行什麼服務器?僅供參考,默認情況下,Maven不會將src/main/java中的任何內容複製到您的應用程序中。因此,你應該保持log4j.properties等中的src/main /資源 – 2014-10-28 13:48:27
我使用了Spring嵌入Tomcat的,我跑我Application.java作爲Java應用程序啓動我的應用程序。順便說一句,我也複製到性能的webapp/WEB-INF,但沒有運氣:( – Jaskey 2014-10-28 13:54:49
log4j.properties必須是可用的類路徑上,WEB-INF目錄不是類路徑上(正常)。如果您運行在命令行上'-Dlog4j.debug'應用程序,你會發現怎麼想的log4j正在發生的事情(如果有的話在所有)。 – 2014-10-28 14:06:19