简体   繁体   English

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

[英]Spring (Boot) ignores my log4j.properties file

I wanted to use logging in my implementation. 我想在我的实现中使用日志记录。 I've set the log4j.properties to logging in both the console and the file: 我已将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

And here's my main application class: 这是我的主要应用程序类:

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);
    }
}

Now, it doesn't matter in which folder of my project I put the properties, or even completely remove it - the logs will show up in my console, but no file is created. 现在,在我的项目的哪个文件夹中放置属性,甚至完全删除它都没关系 - 日志将显示在我的控制台中,但是没有创建文件。
This and the fact I can completely remove the properties make me suspect that Spring Boot is just flat out ignoring the properties file and just using a basic configuration. 这和我可以完全删除属性的事实使我怀疑Spring Boot只是忽略了属性文件而只是使用基本配置。

Does anyone see where I'm going wrong here? 有谁看到我在哪里错了?

UPDATE: I followed the answer and now the file is on my C drive while I thought it would be in my project. 更新:我按照答案,现在文件在我的C驱动器上,而我认为它将在我的项目中。 Is this supposed to happen or did I go wrong? 这应该发生还是我错了?
Also, when I want to log from another class like my ThermostatController it doesn't add that to the log files... What am I doing wrong here? 另外,当我想从像我的ThermostatController这样的另一个类登录时,它不会将它添加到日志文件中...我在这里做错了什么?
Here's part of the ThermostatController class: 这是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";
    }
}

EDIT: Here's my pom.xml just to clarify. 编辑:这是我的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>

I had the same problem, and the follow solution didn't help me ... 我有同样的问题,以下解决方案没有帮助我 ...

<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>

... so I tried replace it with next one ...所以我尝试用下一个替换它

    <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>

and it worked! 它工作了! ... If issue is still actual - try it! ......如果问题仍然存在 - 试试吧! Maybe this way will help you. 也许这种方式会对你有所帮助。

Your configuration seems to be right. 您的配置似乎是正确的。 If you have put the properties file in the right path, then there may be something wrong with your dependencies. 如果您已将属性文件放在正确的路径中,那么您的依赖项可能有问题。 Then replace your dependency with the following and try: 然后用以下内容替换您的依赖项并尝试:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM