我想在我的實現中使用日誌記錄。我設置了log4j.properties到控制檯和文件都記錄:Spring(引導)忽略我的log4j.properties文件
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, CONSOLE, FILE
# CONSOLE is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# CONSOLE uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=${catalina.home}/logs/smartcv.log
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
這是我的主應用程序類:
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class SmartcvfrontApplication {
final static Logger logger = Logger.getLogger(SmartcvfrontApplication.class);
public static void main(String[] args) {
logger.debug("Debug log message");
logger.info("Info log message");
logger.error("Error log message");
SpringApplication.run(SmartcvfrontApplication.class, args);
}
}
現在,它並不重要,其中的文件夾我的項目,我把屬性,甚至完全刪除它 - 日誌將顯示在我的控制檯,但沒有創建文件。
這和事實我可以完全刪除的屬性讓我懷疑Spring Boot只是平淡忽視屬性文件,只是使用基本配置。
有沒有人看到我要去哪裏錯了?
更新:我跟着答案,現在該文件在我的C盤,而我認爲這將在我的項目。這是應該發生還是我出錯了?
另外,當我想從我的ThermostatController這樣的其他類中登錄時,它不會將這些添加到日誌文件中......我在這裏做錯了什麼?
這裏的ThermostatController類的一部分:
@Controller
public class ThermostatController {
final static Logger logger = Logger.getLogger(ThermostatController.class);
// Temperatures
private static int currentTemperature;
private static int chosenTemperature;
@GetMapping("/")
public String loadThermostatView(Model model) {
logger.info("Load ThermostatView");
model.addAttribute("currentTemperature", getcurrentTemp());
model.addAttribute("chosenTemperature", getchosenTemperature());
return "thermostatView";
}
}
編輯:這是我的pom.xml只是爲了澄清。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cgimasterclass.teamdice.smartcv.frontend</groupId>
<artifactId>smartcvfront</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>smartcvfront</name>
<description>Smart CV front end</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mobile</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
將'log4j.properties'文件放在'src/resources'文件夾下。 –
@KrishnanunniPV沒有,仍然沒有日誌文件。 – Kailayla
那麼你可以在'application.properties'文件中添加日誌配置。 –