【问题标题】:Spring: Log4j doesn't write to logging fileSpring:Log4j 不写入日志文件
【发布时间】:2017-09-22 15:56:27
【问题描述】:

我使用 Spring STS 设计了一个 Boot 应用程序。 现在,我想在这里和那里添加一些日志记录。 然后,在this tutorial之后,我写了一个logforj4.properties文件:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender

# Set the name of the file
log4j.appender.FILE.File=log.out

# 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, should not overwrite
log4j.appender.FILE.Append=true

# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=5KB

# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

我用依赖项修改了 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.example</groupId>
    <artifactId>CarsDB</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>CarsDB</name>
    <description>Demo project for Spring Boot</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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.13.1.1</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-http</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-solr</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
      </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

我写了一个类来启动我的应用程序,添加一些代码来测试记录器:

package it.CarsDB.boot;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class CarsDBApplication {

    /* Get actual class name to be printed on */
    static Logger log = Logger.getLogger(CarsDBApplication.class.getName());

    public static void main(String[] args) throws Exception{
        log.debug("Hello this is a debug message");
        log.info("Hello this is an info message");
        SpringApplication.run(CarsDBApplication.class, args);
    }
}

我把log4j.properties放在src/main/java里面,如图(其实我放在src/main/resources >,但它对我的问题没有帮助)。 现在,我希望日志记录被重定向到文件 {$log}/log.out,但是没有创建这样的文件,我只能在控制台上看到日志记录。 我错在哪里了?

感谢您的帮助。

【问题讨论】:

  • 我希望文件在某个地方,而不是您期望的地方。可能在您运行应用程序的工作目录中。您是否尝试在整个文件系统中查找它?
  • 我浏览了整个文件系统,寻找文件 log.out,但该文件尚未创建。
  • Spring 使用 logback,所以实际上你需要 logback.xml 文件。在此处查看示例配置:blog.patouchas.net/technology/…

标签: java spring maven spring-mvc logging


【解决方案1】:

您检查过 Spring Boot 文档吗?

https://logging.apache.org/log4j/2.x/manual/configuration.html

日志配置需要在 src/main/resources 并且应该被命名 log4j2-spring.xmllog4j2.xml

Log4j2 仍然支持属性格式,不完全确定它是否也适用于 Spring Boot。

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2014-05-16
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多