log4j 日志入库
日志入库,有时候真有这需要,看业务吧。。。。
技术上LOG4J对此提供了支持,org.apache.log4j.jdbc.JDBCAppender ,但没提供连接池,在实际应用中可自己扩展连接池。。。。。
主要配置vkj
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<!-- 控制台 -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
</layout>
<!--限制输出级别 -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="ERROR" />
<param name="LevelMin" value="TRACE" />
</filter>
</appender>
<!-- 输出到日志文件 每天一个日志 -->
<appender name="filelog_daily" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/javalog/app_logs/log.log" />
<param name="DatePattern" value="'log.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss} %-5p] [%t] (%c:%L) - %m%n" />
</layout>
</appender>
<!-- 输出到数据库 -->
<appender name="DATABASE" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="URL" value="jdbc:mysql://my-mnt-wrt.vip.mbbidc.net:3306/db_app_logs" />
<param name="driver" value="com.mysql.jdbc.Driver" />
<param name="user" value="db_app_log" />
<param name="password" value="M0kTOAm0qFzsqNtXBRTs" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="INSERT INTO app_log (log_level,log_time,app_id,log_detail,server_ip,log_err_location) VALUES ('%p','%d{yyyy-MM-dd HH:mm:ss}',1000,'%c-%m%n','127.0.0.1','%l')" />
</layout>
<!--限制输出级别 -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="ERROR" />
<param name="LevelMin" value="WARN" />
</filter>
</appender>
<!-- 是否需要配置缓冲区 -->
<!--
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="256" />
<appender-ref ref="DATABASE" />
</appender>
-->
<root>
<priority value="debug" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="filelog_daily" />
<appender-ref ref="DATABASE" />
</root>
</log4j:configuration>