Spring Boot – Logging

As is the case with any new framework (or major addition to an existing and well known framework) the best way to approach it, work with it and learn it is not to dive head first but rather take baby steps in areas that feel more accessible and can link with ideas/processes/steps you are already following.

Spring Boot is sporting several starter recipes that can speedup your configuration and can be consumed separately to each other.

Let’s first talk about logging.

The best practise is to code against SLF4J, provide a desired implementation (preferably Logback), and include in the classpath SLF4J bridges for JUL, JCL and Log4J.

The process is described here by the person/team that authored SLF4J, Log4J, Log4j2, Logback and all the 3 SLF4J bridges namely for JUL, JCL, and Log4J.

The design we described and we are aiming at is also well depicted in the link above and is provided here for quick reference:

Screen Shot 2015-07-12 at 21.03.18

Let’s now get back to Spring Boot.

The Spring Boot dependency module is the following:


That is actually doing all we want and described above effortlessly leaving our classpath changed by this much:

Screen Shot 2015-07-12 at 21.09.39

Log4j Cheet Sheet

A gather-up of Appenders, Layout information and other Configuration concerning Log4j:

# =========
# =========
# Appender | Description
# ------------------------------------------
# ConsoleAppender | Logs to the console
# FileAppender | Logs to a file
# RollingFileAppender | Logs to a file and creates a backup when the file reaches a specified size
# DailyRollingFileAppender | Logs to a file, which is rolled over to a backup file at a specified time
# JDBCAppender | Logs to a database
# NTEventLogAppender | Logs to the Windows event log (available only on Windows)
# SMTPAppender | Logs using the SMTP mail server (sends email)
# SocketAppender | Logs to a TCP socket

# ======
# ======

# Specifier | Description
# ----------------------------------------------------------
# %c | Name of the logger. (In previous versions of log4j, loggers were called categories; hence the abbreviation.)
# %d | Date and time. The default format is ISO8601.
# %m | Message passed by the logger.
# %n | Platform-dependent new line string. (Depending on the platform, it may be "\r\n", "\n", or "\r".)
# %p | Priority of the message.
# %r | Elapsed time, in milliseconds, since the application was started.
# %t | Name of the thread.
# %% | Percent sign.
# %C | Fully qualified name of the class.
# %F | Filename.
# %l | Location information. Depending on JVM, may include fully qualified name of method, source file
# name and line number. If this is the last specifier before %n in a layout, the
# message will provide a hotlink to the source code in the the Eclipse console view.
# %L | Line number.

# log4j format | SimpleDateFormat style pattern | Sample printout
# -----------------------------------------------------------------------
# ABSOLUTE | hh:mm:ss,SSS | 18:16:10,432
# DATE | dd MMM YYYY hh:mm:ss,SSS | 08 Jan 2003 18:16:10,432
# ISO8601 | YYYY-mm-dd hh:mm:ss,SSS | 2003-01-08 18:16:10,432

# =================
# =================

# log4j.rootLogger=PriorityLevel, Appender1 [, Appender2 [, etc.]]
# log4j.appender.KeyName[.Property[.Property[.etc]]={Class|Value}

# Set root logger to DEBUG and assign two appenders
# log4j.rootLogger=PriorityLevel, Appender1 [, Appender2 [, etc.]]
log4j.rootLogger=DEBUG, myConsole, myLogFile

# log4j.appender.KeyName[.Property[.Property[.etc]]={Class|Value}
# Console appender
log4j.appender.myConsole.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling file appender
# max backups at a time
log4j.appender.myLogFile.layout.ConversionPattern= %d{MMM d, yyyy hh:mm:ss a}: %p [%t] %m%n
# Overriding the default DEBUG per appender