To asynchronously save a log to a database, you first need to create a database and then create three fixed tables:
Https://github.com/xiaorenwu-dashijie/logback.git
<?XML version= "1.0" encoding= "UTF-8"?><ConfigurationDebug= "true"> <!--define the storage address of the log file do not use relative paths in Logback configuration - < Propertyname= "Log_home"value= "/home/logs" /> <!--Console Output - <Appendername= "STDOUT"class= "Ch.qos.logback.core.ConsoleAppender"> <Encoderclass= "Ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--formatted output:%d represents the date,%thread represents the thread name,%-5level: the level displays 5 character widths from the left%msg: Log messages,%n are newline characters - <pattern>%X{IP}%d{yyyy-mm-dd HH:mm:ss. SSS} [%thread]%-5level%logger{150}-%msg%n</pattern> </Encoder> </Appender> <!--Info Log Appender - <Appendername= "INFO"class= "Ch.qos.logback.core.rolling.RollingFileAppender"> <Rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--Roll back daily by day - <Filenamepattern>${log_home}//security-info-%d{yyyy-mm-dd}.log</Filenamepattern> <!--Log the largest history 60 days - <maxhistory>60</maxhistory> </Rollingpolicy> <Encoder> <pattern>%X{IP}%d{hh:mm:ss. SSS} [%thread]%-5level%logger-%msg%n</pattern> </Encoder> <Filterclass= "Ch.qos.logback.classic.filter.LevelFilter"><!--Print error log only - < Level>INFO</ Level> <Onmatch>ACCEPT</Onmatch> <Onmismatch>DENY</Onmismatch> </Filter> </Appender> <!--error Log Appender - <Appendername= "ERROR"class= "Ch.qos.logback.core.rolling.RollingFileAppender"> <Rollingpolicyclass= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--Roll back daily by day - <!--Log.dir in Maven profile - <Filenamepattern>${log_home}//security-error-%d{yyyy-mm-dd}.log</Filenamepattern> <!--Log the largest history 60 days - <maxhistory>60</maxhistory> </Rollingpolicy> <Encoder> <pattern>%X{IP}%d{hh:mm:ss. SSS} [%thread]%-5level%logger-%msg%n</pattern> </Encoder> <Filterclass= "Ch.qos.logback.classic.filter.LevelFilter"><!--Print error log only - < Level>ERROR</ Level> <Onmatch>ACCEPT</Onmatch> <Onmismatch>DENY</Onmismatch> </Filter> </Appender> <!--log asynchronously to the database - <Appendername= "DB"class= "Ch.qos.logback.classic.db.DBAppender"> <Connectionsourceclass= "Ch.qos.logback.core.db.DataSourceConnectionSource"> <DataSourceclass= "Org.apache.tomcat.jdbc.pool.DataSource"> <Driverclassname>Com.mysql.jdbc.Driver</Driverclassname> <URL>Jdbc:mysql://**.***.*.**:3306/log_repository?characterencoding=utf-8</URL> <username>*****</username> <Password>*****</Password> </DataSource> </Connectionsource> </Appender> <!--output SQL logs from the debug level of the Com.logback.test.dao package to the console and file - <!--The additivity property is False, indicating that the print information for this loger is no longer passed to the ancestor (root); If true, the console will output two times the log under the DAO package - <Loggername= "Com.logback.test.dao" Level= "DEBUG"additivity= "false"> <Appender-refref= "STDOUT" /> </Logger> <!--Log logging of the info level under the Com.logback.test package to the database - <Loggername= "Com.logback.test" Level= "INFO"additivity= "true"> <Appender-refref= "DB"/> </Logger> <!--output All packages under Debug level logs to the console and file - <Root Level= "INFO"> <Appender-refref= "STDOUT" /> <Appender-refref= "INFO" /> <Appender-refref= "ERROR"/> </Root></Configuration>
When logging, we may have this requirement, for each record in addition to know the log information, but also to record access to the system's IP.
The MDC provides this functionality by simply adding the following method to the code
Mdc.put ("IP", IP);
In the configuration file, you can get it in the following way
Use of Logback.xml to asynchronously save logs to the database