Spring Boot: How can I set the logging level with

2019-01-16 01:06发布

This is very simple question, but I cannot find information.
(Maybe my knowledge about Java frameworks is severely lacking)

How can I set the logging level with application.properties?
And logging file location, etc?

10条回答
趁早两清
2楼-- · 2019-01-16 01:09

Update: Starting with Spring Boot v1.2.0.RELEASE, the settings in application.properties or application.yml do apply. 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. You simply have to use the normal configuration for your logging framework (log4j, logback) for that. Add the appropriate config file (log4j.xml or logback.xml) to the src/main/resources directory and configure to your liking.

You can enable debug logging by specifying --debug when starting the application from the command-line.

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. (This is also recommended from the default logback.xml in Spring Boot.

<include resource="org/springframework/boot/logging/logback/base.xml"/>     
查看更多
成全新的幸福
3楼-- · 2019-01-16 01:11

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. That way I can configure every loglevel within one file, log4j.xml (in my case) that is.

To achieve this you simply have to add those lines to your 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.
That's all, now you're all set to configure logging for boot within your log4j config file!

查看更多
姐就是有狂的资本
4楼-- · 2019-01-16 01:18

Making sure Dave Syer tip gets some love, because adding debug=true to application.properties will indeed enable debug logging.

查看更多
倾城 Initia
5楼-- · 2019-01-16 01:21

In case of eclipse IDE and your project is maven, remember to clean and build the project to reflect the changes.

查看更多
该账号已被封号
6楼-- · 2019-01-16 01:23

For the records: the official documentation, as for Spring Boot v1.2.0.RELEASE and 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, e.g.

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

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. By default Spring Boot picks up the native configuration from its default location for the system (e.g. classpath:logback.xml for Logback), but you can set the location of the config file using the "logging.config" property.

查看更多
太酷不给撩
7楼-- · 2019-01-16 01:24

The proper way to set the root logging level is using the property logging.level.root. See documentation, which has been updated since this question was originally asked.

Example:

logging.level.root=WARN
查看更多
登录 后发表回答