What is the method for configuring Spring Boot logs?
In Spring Boot, you can configure logging using the following methods:
- Configure logging properties using the application.properties or application.yml file, for example:
- application.properties:
# Set the log level to DEBUG for com.example package
# Specify the directory for log output
logging.file=/path/to/logfile.log# Specify the format for log output on console
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} – %msg%n - In the application.yml file, the logging level for com.example is set to DEBUG. The log will be stored in the file /path/to/logfile.log and the console will display logs with the format “%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} – %msg%n”.
- Use Logback or Log4j2 as the logging framework, and place the corresponding configuration file (logback.xml or log4j2.xml) in the classpath to configure the logging.
- Example of logback.xml:
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} – %msg%n
- Sample log4j2.xml configuration:
- Use logging annotations in the code to record logs, such as using the @Slf4j annotation in a class, and then log the logs using methods like log.debug(), log.info(), and so on.
- Include the slf4j annotation from Lombok and create a class called ExampleClass with a method doSomething that logs debug and info messages.
These methods can be used either separately or in combination, depending on the needs, to configure the logs in the appropriate way.