首先先创建三张表,按照自己的需求创建
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j=‘http://jakarta.apache.org/log4j/‘> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}] %X{ip} %t %p %c————%X{name} %m%n" /> </layout> </appender> <appender name="INFO" class="org.apache.log4j.jdbc.JDBCAppender"> <param name="URL" value="jdbc:mysql://IP:3306/log_repository?useUnicode=true&characterEncoding=UTF-8"/> <param name="user" value="****"/> <param name="password" value="****"/> <param name="driver" value="com.mysql.jdbc.Driver"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="INSERT INTO sys_log_info(userId,name,log_time,log_level,location,message)VALUES(‘%X{userId}‘,‘%X{name}‘,‘%d{yyyy-MM-dd HH:mm:ss}‘,‘%p‘,‘%l‘,‘%m‘)"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <!-- 设置日志输出的最小级别 --> <param name="LevelMin" value="INFO" /> <!-- 设置日志输出的最大级别 --> <param name="LevelMax" value="INFO" /> </filter> </appender> <appender name="WARN" class="org.apache.log4j.jdbc.JDBCAppender"> <param name="URL" value="jdbc:mysql://IP:3306/log_repository?useUnicode=true&characterEncoding=UTF-8"/> <param name="user" value="****"/> <param name="password" value="****"/> <param name="driver" value="com.mysql.jdbc.Driver"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="INSERT INTO sys_log_warn(userId,name,log_time,log_level,location,message)VALUES(‘%X{userId}‘,‘%X{name}‘,‘%d{yyyy-MM-dd HH:mm:ss}‘,‘%p‘,‘%l‘,‘%m‘)"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <!-- 设置日志输出的最小级别 --> <param name="LevelMin" value="WARN" /> <!-- 设置日志输出的最大级别 --> <param name="LevelMax" value="WARN" /> </filter> </appender> <appender name="ERROR" class="org.apache.log4j.jdbc.JDBCAppender"> <param name="URL" value="jdbc:mysql://IP:3306/log_repository?useUnicode=true&characterEncoding=UTF-8"/> <param name="user" value="****"/> <param name="password" value="****"/> <param name="driver" value="com.mysql.jdbc.Driver"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="INSERT INTO sys_log_error(userId,name,log_time,log_level,location,message)VALUES(‘%X{userId}‘,‘%X{name}‘,‘%d{yyyy-MM-dd HH:mm:ss}‘,‘%p‘,‘%l‘,‘%m‘)"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <!-- 设置日志输出的最小级别 --> <param name="LevelMin" value="ERROR" /> <!-- 设置日志输出的最大级别 --> <param name="LevelMax" value="ERROR" /> </filter> </appender> <root> <priority value="debug" /> <appender-ref ref="STDOUT" /> <appender-ref ref="INFO" /> <appender-ref ref="WARN" /> <appender-ref ref="ERROR" /> </root> </log4j:configuration>