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
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
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<substitutionPropertyname="logbase"value="#123;user.dir}/logs/ "/> <!-- File output log (file size policy for file output, more than the specified size to file backup) --> <appendername="logfile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Encoding>UTF-8</Encoding> <rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <File>#123;logbase}%d{yyyy-MM-dd}.log</File> <FileNamePattern>#123;logbase}.%d{yyyy-MM-dd}.log.zip </FileNamePattern> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>2MB</MaxFileSize> </triggeringPolicy> <layoutclass="ch.qos.logback.classic.html.HTMLLayout"> <pattern>%date%level%thread%10logger%file%line%msg</pattern> </layout> </appender>
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:
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: