Technology: Spring Profiles provides 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 it 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=<profile> to command when we can starting the application using Jar file.
Depending on 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 classpath for application logging configuration.
Some of the default configurations comes out of 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 packaging structure of application, by default one logger will be present will the name root. Every logger will be child of the root logger.
Logger name will be case-sensitive, and they follow hierarchical naming rules. A logger will be parent to another logger if the name is followed by dot(.) is a prefix of the child name.
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 appenders. The log messages are written to more than one appenders, to restriction this we will use extra attribute.
I.e., the result of a log declaration of logger A will move to all the appenders in A and its predecessor.
If the loggers statement is present in inside 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, means it will propagate to its ancestor 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 releasing 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 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 the to 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
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 feature on top it, for example springProfile is the one of the example for profile-based logging, and logback is also integrating with Janino library to provide the conditional based configuration.