461

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?

zeodtr
  • 10,645
  • 14
  • 43
  • 60
  • 3
    For the record, another option is to set the log level as environment variable, for example via the heroku dashboard. In `Settings` -> `Config Vars` set ```logging.level.com.yourpackage``` to the desired level (INFO, ERROR, DEBUG). – LukeSolar Feb 25 '19 at 21:38
  • @LukeSolar I like that idea. Can you please explain how to read the value of environment variables and have it set under `application.properties`, such that it's different from development to testing (or production) environment? Or just redirect us to a link or something. I would appreciate that. – KareemJ Jul 17 '20 at 07:24
  • 1
    Hi @KareemJeiroudi - no need to read the value manually; you can use the pattern `logging.level.my.package.name=DEBUG` in properties file or define the variable in heroku with key 'logging.level.my.package.name' and value 'DEBUG' and that should set your logging level for the defined package. – LukeSolar Aug 18 '20 at 21:25

18 Answers18

503

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"/>     
aemaem
  • 935
  • 10
  • 20
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 9
    Normally anything that you can do on the command line works in an external config file. So debug=true would do it I think. That flag is a little bit special because the logging has to be initialized very early, but I think that would work. – Dave Syer Dec 10 '13 at 07:50
  • 10
    BTW the preferred logging system is logback, and that's what you get by default from the starter poms. So logback.xml is the most common tool to configure logging in a fine grained way. The --debug flag just switches on some selected Spring logging channels. – Dave Syer Dec 10 '13 at 07:54
  • Another tip, spring boot includes a nice [base.xml](https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/org/springframework/boot/logging/logback/base.xml). Which is a nice starting point. (See my extended answer). – M. Deinum Dec 10 '13 at 08:31
  • @M.Deinum Thank you again for your great tip! Now I can change the log level for my own code. – zeodtr Dec 10 '13 at 10:33
  • If I use this base.xml, I get lots of nice logging, but I can't turn it off. I've tried adding after the include, but it doesn't work. If I remove the , everything is quiet. But then I don't know when my app has finished starting. – Matt Raible Jan 31 '14 at 19:25
  • I figured it out. By setting spring.debug=false in application.properties, setting the values in logback.xml actually works. – Matt Raible Jan 31 '14 at 20:57
  • 2
    It seems we can, now. As for Spring v4.1.3.RELEASE (with Spring Boot v1.2.0.RELEASE), the settings in `application.properties` or `application.yml` do apply, as answered by Richard (modulo the `:` or `=` issue---the colon seems to work for me). – Eric Platon Dec 17 '14 at 00:25
  • Also for unmentioned other packages you can write like "logging.level.root=info" – Dharmendrasinh Chudasama Aug 11 '22 at 15:16
134

You can do that using your application.properties.

logging.level.=ERROR -> Sets the root logging level to error
...
logging.level.=DEBUG -> Sets the root logging level to DEBUG

logging.file=${java.io.tmpdir}/myapp.log -> Sets the absolute log file path to TMPDIR/myapp.log

A sane default set of application.properties regarding logging using profiles would be: application.properties:

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

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

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

Richard
  • 1,543
  • 1
  • 9
  • 13
  • 1
    The '=' sign for assignment did not work. The assignment was done with a ':' as separator. `logging.level.:DEBUG` – randominstanceOfLivingThing Sep 11 '14 at 03:21
  • 2
    Strange, I have this setup working in an example project. The ' '(space), '=' and ':' signs treated equally as separator except for cases where separators are used in keys. Are you sure your key didn't contain any white spaces? – Richard Sep 17 '14 at 09:04
  • 1
    Can confirm that ":" works but "=" is ignored, Edit: Spoke to soon, there was a space between "=" and the level. everything works now. – Mike R Jan 21 '15 at 20:07
  • 1
    This don't work in my test with `spring-boot 1.4.0`: `logging.level.=DEBUG` will cause application fail to start, and getting error: `java.lang.ClassCircularityError: java/util/logging/LogRecord` – Eric Sep 19 '16 at 19:13
  • 9
    Seems to me `logging.level.` is just a convenience syntactic shortcut for `logging.level.root`, which could be prefered as (1) less prone to be confused with a typo, (2) potentially more explicit, and (3) working with the `=` assignment sign, which provides more overall consistency to the configuration file. – Alain BECKER Jul 01 '19 at 07:26
102

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
The Gilbert Arenas Dagger
  • 12,071
  • 13
  • 66
  • 80
37

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.

These are different logging levels and its order from minimum << maximum.

OFF << FATAL << ERROR << 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

Hardik Patel
  • 1,033
  • 1
  • 12
  • 16
22

Suppose your application has package name as com.company.myproject. Then you can set the logging level for classes inside your project as given below in application.properties files

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.

For setting the logging file location use

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

shaunthomas999
  • 5,544
  • 2
  • 26
  • 30
17

According to the documentation you can have different logging levels based on java packages.

 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
  • For spring framework package org.springframework DEBUG logging level would be applied
  • For every other package INFO logging level would be applied

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.

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.otherpackage.otherproject
  • com.newpackage.newproject

would all have logging level WARN applied

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
15

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

Kartoch
  • 7,610
  • 9
  • 40
  • 68
oravecz
  • 1,146
  • 11
  • 16
10

With Springboot 2 you can set the root logging Level with an Environment Variable like this:

logging.level.root=DEBUG

Or you can set specific logging for packages like this:

logging.level.my.package.name=TRACE
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Jakudaba
  • 131
  • 1
  • 3
9

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!

buræquete
  • 14,226
  • 4
  • 44
  • 89
atripes
  • 1,683
  • 4
  • 20
  • 23
8
logging:
  level:
    root: INFO
    com.mycompany.myapp: DEBUG
akasha
  • 494
  • 6
  • 4
7

We can also turn on DEBUG log via command line like below:-

java -jar <jar file> --debug
Abhishek K
  • 645
  • 1
  • 11
  • 23
  • 1
    This answer deserves more love. It's effective and temporary, no need to edit a file. – Paul Apr 13 '22 at 15:13
5

You can try setting the log level to DEBUG it will show everything while starting the application

logging.level.root=DEBUG
Ahmed Salem
  • 1,687
  • 22
  • 26
4

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.

Eric Platon
  • 9,819
  • 6
  • 41
  • 48
4

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.

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.

Martin Choraine
  • 2,296
  • 3
  • 20
  • 37
3

If you want to set more detail, please add a log config file name "logback.xml" or "logback-spring.xml".

in your application.properties file, input like this:

logging.config: classpath:logback-spring.xml

in the loback-spring.xml, input like this:

    <?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>
Sheldon Papa
  • 160
  • 1
  • 12
3

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"
}
epox
  • 9,236
  • 1
  • 55
  • 38
2

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

2

In my current config I have it defined in application.yaml like that:

logging:
  level:
    ROOT: TRACE

I am using spring-boot:2.2.0.RELEASE. You can define any package which should have the TRACE level like that.

fascynacja
  • 1,625
  • 4
  • 17
  • 35
  • 2
    This sets up at application level. OP wants trace only for the hibernate logs for which you would require a package level filter in log settings – Sudip Bhandari Dec 08 '19 at 16:56