Spring Boot Logging Example

Logging is an important aspect when developing spring boot applications. Logging makes it easier to debug the issues that occur during the application’s runtime.

Spring boot framework provides logging support by adding spring-boot-starter-logging transitive dependency by default in every spring-boot-starter dependency module. The framework also supports multiple logging libraries like Log4J, Logback, SLF4J, etc.

Also, the Spring boot automatically configures logging based on the log configuration files available in the spring boot application’s classpath.

In this article, we will learn how to use logging while creating spring boot applications.

Table of Contents

Bsic spring boot logging configuration example

Below are few basic logging properties.

#Logging properties:
logging.level.org.springframework.web=debug
logging.level.org.hibernate=debug

In the above configuration, we are setting hibernate and spring application logs to debug level.

Few of the available configuration properties are

  • logging.path: We can use this property to set the logging file path(Ex: D:/logs/).
  • logging.file: To set the logging file name. We can use either this property with a complete file path(Ex: D:/logs/my_app.log) or use the above property, but not both of them together.
  • logging.pattern.dateformat: We can use this to logging the date and time format used while writing spring boot application logs. Example pattern value: dd-MM-yyyy HH:mm:ss.SSS
  • logging.config: We can use this property to set the location of the logging configuration file. The default value is classpath:logback.xml

Using Logback for logging

Spring boot applications support Logback logging by default. To enable Logback logging in our spring boot application, we have to place the log back configuration under the application’s classpath.

The name of the Logback logging configuration can be logback.xml or logback-spring.xml.

The Logback logging requires no additional configuration, as the framework automatically configures the logging for us.

The below configuration file shows an example Logback configuration.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <springProfile name="default">
        <appender name="CONSOLE"
            class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%msg%n</pattern>
            </encoder>
        </appender>
        <appender name="FILE"
            class="ch.qos.logback.core.FileAppender">
            <file>myApp_default.log</file>
            <encoder>
                <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
            </encoder>
        </appender>
        <root level="DEBUG">
            <appender-ref ref="CONSOLE" />
        </root>
        <root level="DEBUG">
            <appender-ref ref="FILE" />
        </root>
    </springProfile>
    <springProfile name="PROD">
        <appender name="CONSOLE"
            class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%msg%n</pattern>
            </encoder>
        </appender>
        <appender name="FILE"
            class="ch.qos.logback.core.FileAppender">
            <file>myApp_prod.log</file>
            <encoder>
                <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
            </encoder>
        </appender>
        <root level="DEBUG">
            <appender-ref ref="CONSOLE" />
        </root>
        <root level="INFO">
            <appender-ref ref="FILE" />
        </root>
    </springProfile>
</configuration>

In the above Logback configuration file, we have created two different configurations for spring boot profiles: default and PROD.

Also, we can find more logging configurations related to Logback logging is available here.

Log4J Example in spring boot application

We have to exclude the spring-boot-starter-logging dependency to use Log4J or Log4J2 logging with the spring boot application.

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

Next, we can include the required starter dependency as given below.

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

Now we can use the log4j.properties or the log4j.xml configuration files to configure the logging of the spring boot application.

Also, we should place the logging configuration file under the spring boot application classpath.

Sample log4j.properties configuration file is given below. Here we have used both Console appender and File appenders for logging.

log4j.rootLogger=DEBUG,fout
log4j.logger.com.endeca=INFO
log4j.logger.com.endeca.itl.web.metrics=INFO

#Console uppender properties:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m%n

#File uppender properties:
log4j.appender.fout=org.apache.log4j.FileAppender
log4j.appender.fout.File=myapp_log4j.log
log4j.appender.fout.layout=org.apache.log4j.PatternLayout
log4j.appender.fout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n

Conclusion

Logging is a very important part of any application, which may help in debugging errors, logging important information, etc.

In this article, we learned about spring boot logging configurations and how spring boot supports various logging frameworks.

We also learned that the Spring boot framework supports multiple logging libraries. For most cases, Logback is sufficient as it has most of the features supported by other logging libraries.

Finally, the source code is available here. Happy coding 🙂

Spring Boot Logging Example
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading