码迷,mamicode.com
首页 > 数据库 > 详细

Log4j.xml分级别存储日志到数据库

时间:2018-02-09 15:03:14      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:location   amp   dtd   存储   输出   char   osi   password   ons   

首先先创建三张表,按照自己的需求创建

<?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&amp;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&amp;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&amp;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>

 

Log4j.xml分级别存储日志到数据库

标签:location   amp   dtd   存储   输出   char   osi   password   ons   

原文地址:https://www.cnblogs.com/java-spring/p/8434238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!