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?
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"/>
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 DEBUGlogging.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 ;) ).
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
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
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
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!
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.
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 :
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
logging.level.tomcat=TRACE
It's nice feature which brings more flexibility.
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"
}
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>
Settings
->Config Vars
setlogging.level.com.yourpackage
to the desired level (INFO, ERROR, DEBUG). – LukeSolarapplication.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. – Kareem Jeiroudilogging.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