log4j写入数据库
转发自http://www.cnblogs.com/adolfmc/p/3432720.html
Log4j 配置数据库连接池(将日志信息保存到数据库)
org.apache.log4j.jdbc.JDBCAppender 是利用传统的 JDBC 连接方法,这种方式连接数据库效率低下,为了解决这个问题,现在自定义一个 Log4j 的 Appender, 将数据库连接改为连接池的形式,此 Appender 继承自 org.apache.log4j.jdbc.JDBCAppender, 并运用了开源项目Poolman(可从http://nchc.dl.sourceforge.net/project/poolman/PoolMan/poolman-2.1-b1/poolman-2.1-b1.zip 下载)数据库连接池的包。
org.apache.log4j.jdbc.JDBCAppender 的官方 API(http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/jdbc/JDBCAppender.html) 中有这么一段话:
WARNING: This version of JDBCAppender is very likely to be completely replaced in the future. Moreoever, it does not log exceptions . The JDBCAppender provides for sending log events to a database.
Each append call adds to an ArrayList buffer. When the buffer is filled each log event is placed in a sql statement (configurable) and executed. BufferSize , db URL , User , & Password are configurable options in the standard log4j ways.
The setSql(String sql) sets the SQL statement to be used for logging -- this statement is sent to aPatternLayout (either created automaticly by the appender or added by the user). Therefore by default all the conversion patterns in PatternLayout can be used inside of the statement. (see the test cases for examples)
Overriding the getLogStatement(org.apache.log4j.spi.LoggingEvent)method allows more explicit control of the statement used for logging.
For use as a base class:
- Override
getConnection()to pass any connection you want. Typically this is used to enable application wide connection pooling. - Override
closeConnection(Connection con)-- if you override getConnection make sure to implementcloseConnectionto handle the connection you generated. Typically this would return the connection to the pool it came from. - Override
getLogStatement(LoggingEvent event)to produce specialized or dynamic statements. The default uses the sql option value.
大概意思就是建议我们把其提供的 org.apache.log4j.jdbc.JDBCAppender 作为基类来使用,然后 Override 这三个方法: getConnection()、 closeConnection(Connection con) 和 getLogStatement(LoggingEvent event)。
下面是我写的一个 org.apache.log4j.jdbc.JDBCAppender 的子类:
- package com.hmw.log4j;
- import java.sql.Connection;
- import java.sql.SQLException;
- import org.apache.log4j.spi.ErrorCode;
- import com.codestudio.sql.PoolMan;
- /**
- * Log4j的 Appender, 通过连接池获取数据库连接
- * @author Carl He
- */
- public class MyJDBCAppender extends org.apache.log4j.jdbc.JDBCAppender {
- /**通过 PoolMan 获取数据库连接对象的 jndiName 属性*/
- protected String jndiName;
- /**数据库连接对象*/
- protected Connection connection;
- public MyJDBCAppender() {
- super();
- }
- @Override
- protected void closeConnection(Connection con) {
- try {
- if (connection != null && !connection.isClosed())
- connection.close();
- } catch (SQLException e) {
- errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE);
- }
- }
- @Override
- protected Connection getConnection() throws SQLException {
- try {
- //通过 PoolMan 获取数据库连接对象(http://nchc.dl.sourceforge.net/project/poolman/PoolMan/poolman-2.1-b1/poolman-2.1-b1.zip)
- Class.forName("com.codestudio.sql.PoolMan");
- connection= PoolMan.connect("jdbc:poolman://" + getJndiName());
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return connection;
- }
- /**
- * @return the jndiName
- */
- public String getJndiName() {
- return jndiName;
- }
- /**
- * @param jndiName the jndiName to set
- */
- public void setJndiName(String jndiName) {
- this.jndiName = jndiName;
- }
- }
log4j.properties 文件中加上如下一段代码(对此 Appender 的配置):
- log4j.appender.JDBC=com.hmw.log4j.MyJDBCAppender
- log4j.appender.JDBC.jndiName=log
- log4j.appender.JDBC.layout=org.apache.log4j.PatternLayout
- log4j.appender.JDBC.sql=INSERT INTO LOGGING (log_date, log_level, location, message) VALUES ('%d{ISO8601}', '%-5p', '%C,%L', '%m')
最后不要忘了 PoolMan 的配置文件 poolman.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <poolman>
- <management-mode>local</management-mode>
- <datasource>
- <dbname>log</dbname>
- <jndiName>log</jndiName>
- <driver>com.mysql.jdbc.Driver</driver>
- <url>jdbc:mysql://localhost:3306/test</url>
- <username>use</username>
- <password>password</password>
- <minimumSize>0</minimumSize>
- <maximumSize>10</maximumSize>
- <logFile>logs/mysql.log</logFile>
- </datasource>
- </poolman>
log4j写入数据库的更多相关文章
- Log4j写入数据库详解
log4j是一个优秀的开源日志记录项目,我们不仅可以对输出的日志的格式自定义,还可以自己定义日志输出的目的地,比如:屏幕,文本文件,数据库,甚至能通过socket输出.本节主要讲述如何将日志信息输入到 ...
- log4j日志写入数据库
# log4j写入数据库 ### 前言-----------------------------log4j是写入日志到控制台和文件很常见,但是写入到数据库不多见.做性能测试写入到数据库,统计方便些. ...
- (OAF)jdeveloper集成log4j并将日志输出到指定文件并写入数据库
参考: How to configure Log4j in JDev 11g Ever wanted to use log4j in your adf project ? Well though Or ...
- 使用log4j将日志写入数据库并发送邮件
参考: 快速了解Log4J 1.log4j的初始配置 参考该问的配置即可完整的实现写入数据库及发送邮件的功能 a.写入数据库需要配置相应的jar包,数据库类型不同,请使用指定的数据库配置,该文仅限于o ...
- Log4j(一):Log4j将日志信息写入数据库
前言 为了监听一些数据的采集等功能,需要随时监听设备的状态,所以需要运行的时候将日志打入到数据库中. 正文 第一步: 首先是jar包,由于我使用的是springboot,所以,在springboot- ...
- 使用log4j让日志写入数据库
之前做的一个项目有这么个要求,在日志管理系统里,需要将某些日志信息存储到数据库里,供用户.管理员查看分析.因此我就花了点时间搞了一下这一功能,各位请看. 摘要:我们知道log4j能提供强大的可配置的记 ...
- 怎样借助log4j把日志写入数据库中
log4j是一个优秀的开源日志记录项目.我们不仅能够对输出的日志的格式自定义,还能够自定义日志输出的目的地,比方:屏幕.文本文件,数据 库,甚至能通过socket输出.本节使用MySQ ...
- logback日志写入数据库(mysql)配置
如题 建议将日志级别设置为ERROR.这样可以避免存储过多的数据到数据中. 1 logback 配置文件(如下) <?xml version="1.0" encoding ...
- Excel 导入到Datatable 中,再使用常规方法写入数据库
首先呢?要看你的电脑的office版本,我的是office 2013 .为了使用oledb程序,需要安装一个引擎.名字为AccessDatabaseEngine.exe.这里不过多介绍了哦.它的数据库 ...
随机推荐
- Activity启动模式
------siwuxie095 共4种启动模式:standard singleTop singleTask singleInstance 1.标准启动模式(standard) 也即默认的启动模式 ( ...
- Light OJ 1027 - A Dangerous Maze (数学-期望)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题目大意: 一个迷宫, 有n个门,选择一个门花费为|ai|, 如果选择的 ...
- asp.net mvc表单异步提交
html代码: @using (Html.BeginForm("xx", "xx", FormMethod.Post, new { enctype = &quo ...
- 02 Apache Solr: 概览 Solr在信息系统架构中的位置
概述: Apache Solr是一个用JAVA语言构建在Apache Lucene项目上的开源的企业级搜索平台.主要特性包含:全文搜索.命中高亮.片段式搜索.实时索引.动态集群.数据库集成. ...
- C++常用输出 cout、cerr、clog
三者在C++中都是标准IO库中提供的输出工具: cout:写到标准输出的ostream对象: cerr:输出到标准错误的ostream对象,常用于程序错误信息: clog:也是输出标准错误流(这点儿和 ...
- Coursera Machine Learning : Regression 多元回归
多元回归 回顾一下简单线性回归:一个特征,两个相关系数 实际的应用要比这种情况复杂的多,比如 1.房价和房屋面积并不只是简单的线性关系. 2.影响房价的因素有很多,不仅仅是房屋面积,还包括很多其他因素 ...
- 1.jenkins持续集成-jenkins安装
1.为什们要使用jenkins Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目; 2.监控外部调用执行的工作. 2.安装jen ...
- Fatal error in launcher: Unable to create process using '"'
今天遇到了 Fatal error in launcher: Unable to create process using '"' 这个问题,原来是我上次装python3.5的时候,pyth ...
- redis cluster java client jedisCluster spring集成方法
1.使用jedis的原生JedisCluster spring的applicationContext.xml配置redis的连接.连接池.jedisCluster Bean <bean id=& ...
- Merge Intervals 运行比较快
class Solution { public: static bool cmp(Interval &a,Interval &b) { return a.start<b.star ...