Spring Profiles provides a conditional approach to load the parts of the application only in certain environments. It is used to load one of the same types of available beans based on environment configuration.
Using this approach without modifying the source code/configuration will work for all three environments.
When starting our application we can define spring.profiles.active environment variable or we can pass –spring.profiles.active=
Depending on the profile also we can change the logging behavior.
Logback Configuration for Spring Boot Applications:
Spring Boot by default will check for logback-spring.xml in the classpath for application logging configuration.
Some of the default configurations come out of the box with Spring-boot jar file in org/springframework/boot/logging/logback folder, we can re-use these things using include tag by providing the file path.
appenders we used here defined in base.xml which comes spring-boot jar file.
Logback Named Hierarchy:
In general logger name will be the packaging structure of the application, by default one logger will be present will the name root. Every logger will be a child of the root logger.
Logger names will be case-sensitive, and they follow hierarchical naming rules. If the name is followed by a dot (.) The prefix of the child's name then the logger will be the parent for the other logger.
EFor example: if the logger named “org” is the parent of the logger named “org.springframework”, and “root” is the logger resides at the top of the logger hierarchy.
Appenders are attached to the loggers. One logger may connect more than one appender. The log messages are written to more than one appender, to restrict this we will use an extra attributes.
That is, the log declaration of logger A will result in A and all its predecessor's appenders.
If the logger's statement is present inside the org package then A,C appenders will be invoked.
If the logger statement is present in org.springframework.boot then it will only append to C as its additivity is false, which means it will propagate to its ancestors B and A.
Loggers have their Extra flag set to true by default.
Spring Boot Profile based Logging:
While we develop the application in our Java development company, we used to put the log level as DEBUG to log the detailed information, but in production. If keep the same log level size of the log will become huge, and we don’t need the detailed messages, so generally we will keep WARN as log level in production. So, it’s every time while release the build we need to modify the log level if don`t implement profile-based logging.
Spring boot is addressing this using tag in logback XML file which is available from the 1.3 version.
Example configuration using springProfile xml tag:
For dev profile, log messages are appending to the console and for production, profile messages are appending theto file.
There are other ways to do profile-based using Janino library and profile prefixed property file loading in spring boot.
Using Janino Library:
Logback is supporting conditional processing of configuration using this library.
We can use, xml tags in log-back xml file.
Using Janino library the above xml file looks like below:
Spring Boot provides out-of-the-box integration with logback logging framework, and also spring boot is added more features on top of it, for example, springProfile is one of the examples for profile-based logging, and logback is also integrating with Janino library to provide the conditional based configuration.