Since we did not explicitly configure the SpringLoggingHelper class, the default configuration of base.xml file is used. This is because in the application.properties file, we specified DEBUG as the log level for the package that IndexController is part of. In the output, notice that debug and higher level messages of IndexController got logged to the console and file. When you run the main class now and access the application, log messages from IndexController and SpringLoggingHelper are logged to the console and the logs/spring-boot-logging.log file. However, you cannot specify both the logging.file and logging.path properties together. If you use it, Spring Boot creates a spring.log file in the specified path. Note: There is also a logging.path property to specify a path for a logging file. Logging.file=logs/spring-boot-logging.log Here is an example of an application.properties file with logging configurations. You can also define a log file to write log messages in addition to the console. In the application.properties file, you can define log levels of Spring Boot, application loggers, Hibernate, Thymeleaf, and more. In the default structure of a Spring Boot web application, you can locate the application.properties file under the Resources folder. The application.properties file is likely the most popular of several different ways to externalize Spring Boot configuration properties.
In a Spring Boot application, you can externalize configuration to work with the same application code in different environments. Become an expert with Spring Framework 5! Configuration via Spring Boot’s application.properties File As we’ll see in the next section, changing log levels in Spring Boot is very simple. Here you can see the Spring Boot has overridden the default logging level of Logback by setting the root logger to INFO, which is the reason we did not see the debug messages in the example above. Here is the code of the base.xml file from the spring-boot github repo. The base.xml file references both of them.
In addition, Spring Boot provides provide two preconfigured appenders through the console-appender.xml and file-appender.xml files. However, the Spring Boot team provides us a default configuration for Logback in the Spring Boot default Logback configuration file, base.xml. Logback by default will log debug level messages. Notice that the debug messages are not getting logged. The output of both the IndexController and SpringLoggingHelper classes are from the Logback root logger. We haven’t written any configuration for Logback. When the application starts, access it from your browser with the URL, The logging output on the IntelliJ console is this. Run the SpringBootWebApplication main class. Assuming you’re using Maven or Gradle to manage you Spring Boot project, the necessary dependencies are part of the dependencies under Spring Boot. If you are wondering about SLF4J and Logback dependencies, you don’t need to specify any. By writing against SLF4J, our code remains decoupled from Logback, thus providing us the flexibility to plug-in a different logging framework, if required later. SLF4J is a façade for commonly used logging frameworks, such as Java Util Logging, Log4J 2, and Logback. In both the classes above, we wrote logging code against the SLF4J API. Private final Logger logger = LoggerFactory.getLogger(this.getClass())
Here is the code of SpringLoggingHelper : //package Although this class doesn’t do anything except emitting logging statements, it will help us understand configuring logging across different packages. Let’s add a SpringLoggingHelper class with logging code to the application. Logger.error("This is an error message") The application contains a controller called IndexController, to which we’ll add logging code. We’ll configure Logback for this application. In a previous post, I wrote about creating a web application using Spring Boot. Out of the box, Spring Boot makes Logback easy to use. Like many things in Spring Boot, Logback, by default, gets configured with sensible defaults. While there are a number of logging options for Java, the Spring Boot chose to use Logback for the default logger.
In this post, I’ll discuss how to use Logback with Spring Boot. The posts are available as Logback Configuration: using XML and Logback Configuration: using Groovy.
In a series of posts on Logback, I’ve also discussed how to configure Logback using XML and Groovy. I introduced logback in my introductory post, Logback Introduction: An Enterprise Logging Framework. It’s fast, have simple but powerful configuration options, and comes with a small memory footprint. Logback makes an excellent logging framework for enterprise applications.