这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)

如何使用application.properties设置日志级别? 日志文件的位置,等等?


当前回答

如果您想使用不同的日志框架(例如log4j),我发现最简单的方法是禁用spring引导自己的日志记录并实现自己的日志记录。这样我就可以在一个文件log4j.xml(在我的例子中)中配置每个日志级别。

要实现这一点,你只需要将这些行添加到你的pom.xml:

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

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

您可能已经有了第一个依赖项,只需要另外两个依赖项。请注意,这个例子只包含log4j。 以上就是全部内容,现在您就可以在log4j配置文件中为引导配置日志了!

其他回答

如果您使用Spring Boot,那么您可以直接在应用程序中添加以下属性。属性文件设置日志级别, 自定义日志记录模式并将日志存储在外部文件中。

这些是不同的日志级别及其从最小值<<最大值的顺序。

关闭<<致命<<错误<<警告<< info << debug << trace << all

# To set logs level as per your need.
logging.level.org.springframework = debug
logging.level.tech.hardik = trace

# To store logs to external file
# Here use strictly forward "/" slash for both Windows, Linux or any other os, otherwise, its won't work.      
logging.file=D:/spring_app_log_file.log

# To customize logging pattern.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

请通过这个链接来更生动地定制您的日志。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

如果您想使用不同的日志框架(例如log4j),我发现最简单的方法是禁用spring引导自己的日志记录并实现自己的日志记录。这样我就可以在一个文件log4j.xml(在我的例子中)中配置每个日志级别。

要实现这一点,你只需要将这些行添加到你的pom.xml:

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

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

您可能已经有了第一个依赖项,只需要另外两个依赖项。请注意,这个例子只包含log4j。 以上就是全部内容,现在您就可以在log4j配置文件中为引导配置日志了!

确保Dave Syer技巧得到一些喜爱,因为向应用程序添加debug=true。属性确实会启用调试日志记录。

in spring boot project we can write logging.level.root=WARN but here problem is, we have to restart again even we added devtools dependency, in property file if we are modified any value will not autodetectable, for this limitation i came to know the solution i,e we can add actuator in pom.xml and pass the logger level as below shown in postman client in url bar http://localhost:8080/loggers/ROOT or http://localhost:8080/loggers/com.mycompany and in the body you can pass the json format like below

{
  "configuredLevel": "WARN"
}

您可以尝试将日志级别设置为DEBUG,它将在启动应用程序时显示所有内容

logging.level.root=DEBUG