spring-boot-slf4j-logging-example

By default, SLF4j Logging is included in the Spring Boot web application, you just need to enable it.

application.properties

spring-boot-web-project$ mvn dependency:tree

+...
+- org.springframework.boot:spring-boot-starter-logging:jar:1.4.2.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.7:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.7:compile
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.21:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.21:compile
+...
Note
Review this Spring Boot Logback XML template to understand the default logging pattern and configuration.

1. application.properties

To enable logging, create a application.properties file in the root of the resources folder.

1.1 logging.level – define logging level, the logging will be output to console.

application.properties

logging.level.org.springframework.web=ERROR
logging.level.com.mkyong=DEBUG

1.2 logging.file – define logging file, the logging will be output to a file and console.

application.properties

logging.level.org.springframework.web=ERROR
logging.level.com.mkyong=DEBUG

#output to a temp_folder/file
logging.file=${java.io.tmpdir}/application.log

#output to a file
#logging.file=/Users/mkyong/application.log

1.3 logging.pattern – define a custom logging pattern.

application.properties

logging.level.org.springframework.web=ERROR
logging.level.com.mkyong=DEBUG

# Logging pattern for the console
logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

# Logging pattern for file
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"

logging.file=/Users/mkyong/application.log

2. application.yml

This is the equivalent in YAML format.

application.yml

logging:
  level:
    org.springframework.web: ERROR
    com.mkyong: DEBUG
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
    file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
  file: /Users/mkyong/application.log

3. Classic Logback.xml

If you don’t like the Spring Boot logging template, just create a standard logback.xml in the root of the resources folder or root of the classpath. This will override the Spring Boot logging template.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

	<property name="DEV_HOME" value="c:/logs" />

	<appender name="FILE-AUDIT"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${DEV_HOME}/debug.log</file>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<Pattern>
				%d{yyyy-MM-dd HH:mm:ss} - %msg%n
			</Pattern>
		</encoder>

		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- rollover daily -->
			<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>

	</appender>

	<logger name="com.mkyong" level="debug"
		additivity="false">
		<appender-ref ref="FILE-AUDIT" />
	</logger>

	<root level="error">
		<appender-ref ref="FILE-AUDIT" />
	</root>

</configuration>

4. Set Root Level

application.properties

# root logging level, warning : too much output
logging.level.=DEBUG
application.yml

logging:
  level:
    ROOT: DEBUG  

Download Source Code

Download It – spring-boot-web-slf4j-logging.zip (16 KB)

References

  1. Spring Boot – How to Logging
  2. Spring Boot Logback XML template
  3. Spring Boot: How can I set the logging level with application.properties?
  4. logback.xml Examples
  5. Logback Home