How to customize logging and configure info, debug, error in spring boot maven project

Logging

Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available.

By default, if you use the “Starters”, Logback is used for logging. Appropriate Logback routing is also included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J, or SLF4J all work correctly.

There are a lot of logging frameworks available for Java. Do not worry if the above list seems confusing. Generally, you do not need to change your logging dependencies and the Spring Boot defaults work just fine.

When you deploy your application to a servlet container or application server, logging performed via the Java Util Logging API is not routed into your application’s logs. This prevents logging performed by the container or other applications that have been deployed to it from appearing in your application’s logs.


Log Format

The default log output from Spring Boot resembles the following example:



The following items are output:

  • Date and Time: Millisecond precision and easily sortable.
  • Log Level: ERROR, WARN, INFO, DEBUG, or TRACE.
  • Process ID.
  • A --- separator to distinguish the start of actual log messages.
  • Thread name: Enclosed in square brackets (may be truncated for console output).
  • Logger name: This is usually the source class name (often abbreviated).
  • The log message.

Logback does not have a FATAL level. It is mapped to ERROR.
You can also specify debug=true in your application.properties.


When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information. Enabling the debug mode does not configure your application to log all messages with DEBUG level.

Alternatively, you can enable a “trace” mode by starting your application with a --trace flag (or trace=true in your application.properties). Doing so enables trace logging for a selection of core loggers (embedded container, Hibernate schema generation, and the whole Spring portfolio).


Log Levels

All the supported logging systems can have the logger levels set in the Spring Environment (for example, in application.properties) by using logging.level.<logger-name>=<level> where level is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The root logger can be configured by using logging.level.root.

The following example shows potential logging settings in application.properties:

logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error

It’s also possible to set logging levels using environment variables. For example, LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG will set org.springframework.web to DEBUG.


Log Groups


It’s often useful to be able to group related loggers together so that they can all be configured at the same time. For example, you might commonly change the logging levels for all Tomcat related loggers, but you can’t easily remember top level packages.

To help with this, Spring Boot allows you to define logging groups in your Spring Environment. For example, here’s how you could define a “tomcat” group by adding it to your application.properties:

logging.group.tomcat=org.apache.catalina,
org.apache.coyote,
org.apache.tomcat

Once defined, you can change the level for all the loggers in the group with a single line:

logging.level.tomcat=TRACE

Spring Boot includes the following pre-defined logging groups that can be used out-of-the-box:


Web

  • org.springframework.core.codec,
  • org.springframework.http, org.springframework.web,
  • org.springframework.boot.actuate.endpoint.web,
  • org.springframework.boot.web.servlet.ServletContextInitializerBeans

Sql

  • org.springframework.jdbc.core, org.hibernate.SQL




Custom Log Configuration

The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring Environment property: logging.config.

You can force Spring Boot to use a particular logging system by using the org.springframework.boot.logging.LoggingSystem system property. The value should be the fully qualified class name of a LoggingSystem implementation. You can also disable Spring Boot’s logging configuration entirely by using a value of none.

Depending on your logging system, the following files are loaded:


Logback

logback-spring.xml,
logback-spring.groovy,
logback.xml, or logback.groovy


Log4j2

log4j2-spring.xml or log4j2.xml


JDK (Java Util Logging)

logging.properties



Now Let's create the logging xml file with configuration, and I named it as logback.xml in spring boot maven project under the resource package

Here is the project structure: 

Structure before generate log file

Here is Logback.xml: 


Structure after generated log file


Here is the logging file after project was run and then it's generated:


That's it. Hope it help!


Watch Video for How to configure step by step here: 




Previous Post Next Post