gpt4 book ai didi

java - Spring (Boot) 忽略了我的 log4j.properties 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:06 25 4
gpt4 key购买 nike

我想在我的实现中使用日志记录。我已将 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>

最佳答案

我遇到了同样的问题,下面的解决方案对我没有帮助...

<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-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>

...所以我尝试用下一个替换它

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
...
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>

成功了! ... 如果问题仍然存在 - 试试吧!也许这种方式会对你有所帮助。

关于java - Spring (Boot) 忽略了我的 log4j.properties 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46846054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com