- Logging can help to debug your application faster.
- Spring Boot supports various logging framework - logback, log4j2, Java util logging (JUL)
- Logback - It is default logging mechanism in many spring boot application. It offers flexible configuration and good performance. Mostly used.
- log4j2 - It has features like asynchronous and supports various output formats.
- JUL - It is default logging framework included in java standard edition, has less features.
- Logback - to customise the logging configuration we need make the logback-spring.xml or logback.xml file in src/main/resources. It will be used instead on default logging configuration.
- Different logging levels:
- TRACE
- DEBUG
- INFO
- WARN
- ERROR
Slf4jandLog4j2: These are the two annotations provided by spring boot, these helps to automatically inject the logger instances to the classes.- We will be using logback via Slf4j.
- By default ERROR, WARN, INFO are available. To make other logging level enable we need to do some changes.\
- to enable remaining TRACE and DEBUG use :
logging: level: root: DEBUG
- to enable remaining TRACE and DEBUG use :
- How to use?
- We can simply use
@Slf4jannotation at class level and use it as -log.info("some message {} ", username)
- We can simply use
- Using logback.xml:
<configuration> <appender name="myConsoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern> %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <appender name="myFileAppender" class="ch.qos.logback.core.FileAppender"> <encoder> <pattern> %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> <file> journalApp.log </file> </appender> <root level="INFO"> <appender-ref ref="myConsoleAppender" /> <appender-ref ref="myFileAppender" /> </root> </configuration>- Appender - which where to print the logs, basically to tell where to output the logs.
- encoder - shows which format to print the logs.
- right now using fileAppender simply use the same and file and append all the logs, what if we need to make different - different files based on time or size etc.
- User