简体   繁体   English

Spring Boot:如何使用 application.properties 设置日志记录级别?

[英]Spring Boot: How can I set the logging level with application.properties?

This is very simple question, but I cannot find information.这是一个非常简单的问题,但我找不到信息。
(Maybe my knowledge about Java frameworks is severely lacking) (也许我对 Java 框架的了解严重缺乏)

How can I set the logging level with application.properties?如何使用 application.properties 设置日志记录级别?
And logging file location, etc?和日志文件位置等?

Update: Starting with Spring Boot v1.2.0.RELEASE, the settings in application.properties or application.yml do apply.更新:从 Spring Boot v1.2.0.RELEASE 开始, application.propertiesapplication.yml的设置确实适用。 See the Log Levels section of the reference guide.请参阅参考指南的日志级别部分

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

For earlier versions of Spring Boot you cannot.对于早期版本的 Spring Boot,您不能。 You simply have to use the normal configuration for your logging framework (log4j, logback) for that.为此,您只需使用日志框架(log4j、logback)的正常配置。 Add the appropriate config file ( log4j.xml or logback.xml ) to the src/main/resources directory and configure to your liking.将适当的配置文件( log4j.xmllogback.xml )添加到src/main/resources目录并根据您的喜好进行配置。

You can enable debug logging by specifying --debug when starting the application from the command-line.您可以通过在从命令行启动应用程序时指定--debug来启用调试日志记录。

Spring Boot provides also a nice starting point for logback to configure some defaults, coloring etc. the base.xml file which you can simply include in your logback.xml file. Spring Boot 也为 logback 提供了一个很好的起点来配置一些默认值、着色等。 base.xml文件你可以简单地包含在你的 logback.xml 文件中。 (This is also recommended from the default logback.xml in Spring Boot. (这也是从 Spring Boot 中的默认logback.xml中推荐的。

<include resource="org/springframework/boot/logging/logback/base.xml"/>     

You can do that using your application.properties.您可以使用您的 application.properties 来做到这一点。

logging.level.=ERROR -> Sets the root logging level to error logging.level.=ERROR -> 将根日志记录级别设置为错误
... ...
logging.level.=DEBUG -> Sets the root logging level to DEBUG logging.level.=DEBUG -> 将根日志记录级别设置为 DEBUG

logging.file=${java.io.tmpdir}/myapp.log -> Sets the absolute log file path to TMPDIR/myapp.log logging.file=${java.io.tmpdir}/myapp.log -> 设置绝对日志文件路径为 TMPDIR/myapp.log

A sane default set of application.properties regarding logging using profiles would be: application.properties:关于使用配置文件进行日志记录的一组合理的默认 application.properties 将是:application.properties:

spring.application.name=<your app name here>
logging.level.=ERROR
logging.file=${java.io.tmpdir}/${spring.application.name}.log

application-dev.properties:应用程序-dev.properties:

logging.level.=DEBUG
logging.file=

When you develop inside your favourite IDE you just add a -Dspring.profiles.active=dev as VM argument to the run/debug configuration of your app.当您在您最喜欢的 IDE 中进行开发时,您只需将-Dspring.profiles.active=dev作为 VM 参数添加到应用程序的运行/调试配置中。

This will give you error only logging in production and debug logging during development WITHOUT writing the output to a log file.这将使您仅在开发期间登录生产和调试日志记录错误,而无需将输出写入日志文件。 This will improve the performance during development ( and save SSD drives some hours of operation ;) ).这将提高开发过程中的性能(并节省 SSD 驱动器的运行时间;))。

The proper way to set the root logging level is using the property logging.level.root .设置日志记录级别的正确方法是使用属性logging.level.root See documentation , which has been updated since this question was originally asked.请参阅文档,自最初提出此问题以来已更新。

Example:示例:

logging.level.root=WARN

If you are on Spring Boot then you can directly add following properties in application.properties file to set logging level, customize logging pattern and to store logs in the external file.如果您使用的是 Spring Boot,那么您可以直接在 application.properties文件中添加以下属性来设置日志级别、自定义日志模式并将日志存储在外部文件中。

These are different logging levels and its order from minimum << maximum.这些是不同的日志记录级别及其从最小值 << 最大值的顺序。

OFF << FATAL << ERROR << WARN << INFO << DEBUG << TRACE << ALL OFF << 致命 << 错误 << WARN << 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"

Please pass through this link to customize your log more vividly.请通过此链接更生动地自定义您的日志。

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

Suppose your application has package name as com.company.myproject .假设您的应用程序的包名称为com.company.myproject Then you can set the logging level for classes inside your project as given below in application.properties files然后,您可以在 application.properties 文件中为项目中的类设置日志记录级别,如下所示

logging.level.com.company.myproject = DEBUG logging.level.com.company.myproject = DEBUG

logging.level.org.springframework.web = DEBUG and logging.level.org.hibernate = DEBUG will set logging level for classes of Spring framework web and Hibernate only. logging.level.org.springframework.web = DEBUGlogging.level.org.hibernate = DEBUG将只为 Spring 框架 web 和 Hibernate 的类设置日志级别。

For setting the logging file location use用于设置日志文件位置使用

logging.file = /home/ubuntu/myproject.log logging.file = /home/ubuntu/myproject.log

确保 Dave Syer 技巧得到一些喜爱,因为将debug=true添加到 application.properties 确实会启用调试日志记录。

In case you want to use a different logging framework, log4j for example, I found the easiest approach is to disable spring boots own logging and implement your own.如果您想使用不同的日志记录框架,例如 log4j,我发现最简单的方法是禁用 Spring Boots 自己的日志记录并实现您自己的。 That way I can configure every loglevel within one file, log4j.xml (in my case) that is.这样我就可以在一个文件 log4j.xml(在我的情况下)中配置每个日志级别。

To achieve this you simply have to add those lines to your pom.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>

You probably already have the first dependency and only need the other two.您可能已经有了第一个依赖项,只需要另外两个。 Please note, that this example only covers log4j.请注意,此示例仅涵盖 log4j。
That's all, now you're all set to configure logging for boot within your log4j config file!就是这样,现在你已经准备好在你的 log4j 配置文件中配置启动日志了!

With Springboot 2 you can set the root logging Level with an Environment Variable like this:使用 Springboot 2,您可以使用环境变量设置根日志记录级别,如下所示:

logging.level.root=DEBUG

Or you can set specific logging for packages like this:或者您可以为这样的包设置特定的日志记录:

logging.level.my.package.name=TRACE

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

logging.level.root=DEBUG

For the records: the official documentation , as for Spring Boot v1.2.0.RELEASE and Spring v4.1.3.RELEASE:记录: 官方文档,对于 Spring Boot v1.2.0.RELEASE 和 Spring v4.1.3.RELEASE:

If the only change you need to make to logging is to set the levels of various loggers then you can do that in application.properties using the "logging.level" prefix, eg如果您需要对日志记录进行的唯一更改是设置各种记录器的级别,那么您可以使用“logging.level”前缀在 application.properties 中执行此操作,例如

logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR

You can also set the location of a file to log to (in addition to the console) using "logging.file".您还可以使用“logging.file”设置要登录的文件的位置(除了控制台)。

To configure the more fine-grained settings of a logging system you need to use the native configuration format supported by the LoggingSystem in question.要配置日志系统的更细粒度的设置,您需要使用相关 LoggingSystem 支持的本机配置格式。 By default Spring Boot picks up the native configuration from its default location for the system (eg classpath:logback.xml for Logback), but you can set the location of the config file using the "logging.config" property.默认情况下,Spring Boot 从系统的默认位置(例如,用于 Logback 的 classpath:logback.xml)获取本机配置,但您可以使用“logging.config”属性设置配置文件的位置。

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在 spring boot 项目中,我们可以写 logging.level.root=WARN 但这里的问题是,即使我们添加了 devtools 依赖项,我们也必须重新启动,如果我们修改了属性文件中的任何值,将无法自动检测到这个限制,我开始知道解决方案我,我们可以在 pom.xml 中添加执行器并传递记录器级别,如下所示在 url 栏中的邮递员客户端http://localhost:8080/loggers/ROOThttp://localhost:8080/loggers/com .mycompany和正文中,您可以传递如下的 json 格式

{
  "configuredLevel": "WARN"
}

Existing answers are greats.现有的答案很棒。 I just want to share with you a new spring boot feature allowing to group logs and set logging level on the whole group.我只想与您分享一个新的 Spring Boot 功能,它允许对日志进行分组并在整个组上设置日志记录级别。

Exemple from the docs :文档中的示例:

  • Create a logging group创建日志记录组
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
  • Set the logging level for group设置组的日志级别
logging.level.tomcat=TRACE

It's nice feature which brings more flexibility.这是一个很好的功能,带来了更多的灵活性。

If you want to set more detail, please add a log config file name "logback.xml" or "logback-spring.xml".如果你想设置更多细节,请添加一个日志配置文件名“logback.xml”或“logback-spring.xml”。

in your application.properties file, input like this:在 application.properties 文件中,输入如下:

logging.config: classpath:logback-spring.xml

in the loback-spring.xml, input like this:在 loback-spring.xml 中,输入如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/base.xml"/>

        <appender name="ROOT_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>

            <file>sys.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">


                <fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/system.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>500MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

            <encoder>
                <pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
                </pattern>
            </encoder>
        </appender>


        <appender name="BUSINESS_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>TRACE</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>

            <file>business.log</file>

            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

                <fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/business.%d{yyyy-MM-dd}.%i.log</fileNamePattern>

                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>500MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>

            <encoder>
                <pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
                </pattern>
            </encoder>
        </appender>


        <logger name="{project-package-name}" level="TRACE">
            <appender-ref ref="BUSINESS_APPENDER" />
        </logger>

        <root level="INFO">
            <appender-ref ref="ROOT_APPENDER" />
        </root>

    </configuration>

In my current config I have it defined in application.yaml like that:在我当前的配置中,我在application.yaml 中像这样定义了它:

logging:
  level:
    ROOT: TRACE

I am using spring-boot:2.2.0.RELEASE.我正在使用 spring-boot:2.2.0.RELEASE。 You can define any package which should have the TRACE level like that.您可以定义任何应该具有类似 TRACE 级别的包。

logging:
  level:
    root: INFO
    com.mycompany.myapp: DEBUG

如果是 Eclipse IDE 并且您的项目是 maven,请记住清理和构建项目以反映更改。

我们还可以通过命令行打开调试日志,如下所示:-

java -jar <jar file> --debug

According to the documentation you can have different logging levels based on java packages .根据文档,您可以根据 java 包具有不同的日志记录级别

 logging.level.com.mypackage.myproject=WARN
 logging.level.org.springframework=DEBUG
 logging.level.root=INFO 

This would mean that这将意味着

  • For your custom package com.mypackage.myproject WARN logging level would be applied对于您的自定义包com.mypackage.myproject WARN日志记录级别将被应用
  • For spring framework package org.springframework DEBUG logging level would be applied对于 spring 框架包org.springframework将应用DEBUG日志记录级别
  • For every other package INFO logging level would be applied对于每个其他包,将应用INFO日志记录级别

You can also group together different java packages and instruct the system to use the same logging level for all packages of the group in a single line.您还可以将不同的 java 包组合在一起,并在一行中指示系统对该组的所有包使用相同的日志记录级别。

In the previous example you could do在前面的例子中,你可以做

 logging.level.root=INFO 
 logging.level.org.springframework=DEBUG
 
 logging.group.myCustomGroup = com.mypackage.myproject, com.otherpackage.otherproject, com.newpackage.newproject
 logging.level.myCustomGroup=WARN

This would mean that the packages这意味着包

  • com.mypackage.myproject com.mypackage.myproject
  • com.otherpackage.otherproject com.otherpackage.otherproject
  • com.newpackage.newproject com.newpackage.newproject

would all have logging level WARN applied都会应用日志级别WARN

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

相关问题 我可以将 Spring 引导日志记录设置从 application.properties 文件移到未跟踪的文件中吗? - Can I move Spring Boot logging settings out of application.properties file and into an untracked file? Spring日志和application.properties - Spring logging and application.properties 用于记录的Spring Boot应用程序属性 - spring boot application properties for logging 如何通过Spring Boot应用程序中的application.properties禁用WARN消息并仅在日志中启用INFO消息? - How to disable WARN messages and enable only INFO messages in logs through application.properties in Spring boot application? Spring Boot application.properties 中的日志级别问题 - Issues with Log Levels in Spring Boot application.properties 在 Spring Boot 中的 application.properties 中获取用户主路径 - Getting the user home path in application.properties in Spring Boot Spring Boot:Logback不从application.properties中选择值 - Spring Boot :Logback not picking values from application.properties 如何在Spring Boot中设置嵌入式tomcat的日志级别? - How to set the logging level of embedded tomcat in Spring Boot? 获取 spring 引导应用程序中的日志记录级别 - Get the logging level in spring boot application 我可以在Spring Boot应用程序中进行自定义日志记录吗? - Can i do custom logging in spring boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM