On this page we will provide spring boot logging example. Spring boot can use Logback, Log4J2, java util logging. By default spring boot uses Logback for its logging. By default log is logged in console and can also be logged in files. The JAR dependency for Logback is resolved by spring-boot-starter-logging. When we use any spring boot starter then spring-boot-starter-logging is resolved by default. We need not to include it separately. If Logback JAR is available in classpath then spring boot will always choose Logback for logging. So to use other logging such as Log4J2, we need to exclude Logback JAR and add Log4J2 JAR in classpath. To use Logback logging we have to do nothing, just configure logging level in application.properties or application.yml and we are done. By default ERROR, WARN and INFO log level messages are logged in console. To change log level, use logging.level property. To get logs in file, we can configure logging.file or logging.path in property file. Log files will rotate when they reach 10 MB. Find the spring environment that is configured in property file.
logging.level.* : It is used as prefix with package name to set log level.
logging.file : It configures a log file name to log message in file. We can also configure file name with absolute path.
logging.path : It only configures path for log file. Spring boot creates a log file with name spring.log.
logging.pattern.console : It defines logging pattern in console.
logging.pattern.file: It defines logging pattern in file.
logging.pattern.level: It defines the format to render log level. Default is %5p.
logging.exception-conversion-word : It defines conversion word when logging exceptions.
PID : It defines the current process ID.
We will discuss here how to use Logback and configure log properties using application.properties and application.yml and Logback XML file. We will also provide how to enable Log4J2 logging in our spring boot application.