In Java (Spring Boot) applications, the LogBack logging framework is often used to log, or it can be simplified with @Slf4j annotations in Lombok. To customize the output of the log, a configuration file named logback.xml is typically configured in the project.
pom.xml 文件
In the most basic case, a dependency library is required
1 | <dependency> |
If used with lombok, you can define two dependent libraries
1 | <!-- Lombok and Log --> |
Configuration in logback.xml
In maven projects, logback.xml files are typically placed in the project’s src/main/resources directory. Here are some basics to make it easy for you to quickly build your own project log configuration
The most basic configuration:
1 | <configuration> |
In the most basic configuration, we simply output the logs to the screen.
If we need to log the log in a local file, we usually need to add the following configuration information:
1 | <substitutionProperty name="logbase" value="${user.dir}/logs/ "/> |
In the configuration, a variable named logbase is first declared, indicating the directory where the log files are stored. To prevent a single log file from being too large, a rollover mode of log files is declared.
After adding the file configuration, the full logback .xml as follows:
1 | <configuration> |
Of course, we can also configure the log to be written to the database, and the following shows the configuration when storing the mySQL database as the log:
1 | <appender name="DB" class="ch.qos.logback.classic.db.DBAppender"> |