Following diagram demonstrated the process to bootstrap and use Logback for loggings in Spring Boot applciation.
Video version of this explanation
If you’d perfer a more intuitive and vision version, check it out below.
Key steps explained
The first section for logic in SpringBoot Application domain
- When a SpringBoot application start to execute SpringApplicaiton.run , internally it will execute
prepareEnvironment, which will create or load application run environment , such as environment variables.
- SpringBoot use Observer Pattern and SpringApplicationEvent to keep tack of each key steps in Spring Boot Application life cycle. SpringBoot will pass loaded environments as argument to list of registered listeners.
- SpringBoot’s Event publisher (EventPublishingRunListener) will publish an event called ApplicationEnvironmentPreparedEvent via it’s built-in multicast publisher.
- LoggingApplicationListener is one of 6 components in Spring subscribed to ApplicationEnvironmentPreparedEvent. It will configure Spring’s LoggingSystem. If the environment contains a logging.config property, it will be used to bootstrap the logging system, otherwise a default configuration is used.
- SpringBoot will leverage Classloader to try to load any LoggingSystem or its descendants.
- If you added sl4j into class path, LoggingSystemFactory will produce a new LogbackLoggingSystem, which extended LoggingSystem ultimately.
return new LogbackLoggingSystem(classLoader);
Now we move to domain of logging system in slf4j & logback.
- LogbackLoggingSystem will invoke doConfig of SpringBootJoranConfigurator to setup logging sub system. This Configurator is backbone of Joran framework.
Joran: a mature, flexible and powerful configuration framework. On which logback’s configuration logic is based. Details check it out at Joran Configuration framework
- Interpreter will parse configuration details in logback.xml , internally logback would convert elements of aforesaid xml file as a list of Event , then Interpreter has one EventPlayer to process those events one by one.e
- Interpreter is Joran’s main driving class. It extends SAX DefaultHandler which invokes various actions according to predefined patterns (configured as RuleStore ). It leverage Constructor to start a new instance of LogstashEncoder in memory.
- Joran defines StartEvent and EndEvent to process beginning and ending element in XML configuration. For </encoder> , Joran will use reflection to call property setter of ConsolerAppender, and inject aforesaid logstashEncoder as property encoder .
- This is the last step of this setup. When there are logs to be wrote, the log Appender’s internal encoder (that’s LogstashEncoder in this case) will be invoked to render output message.