Log4jdbc demo
package log4jdbc; import java.sql.Connection;
import java.sql.PreparedStatement; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceTest { @Test
public void testC3P0() {
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("密码");
dataSource.setDriverClass("net.sf.log4jdbc.DriverSpy");
dataSource.setJdbcUrl("jdbc:log4jdbc:mysql://localhost:3306/实例名");
dataSource.setMaxPoolSize(50);
Connection conn = dataSource.getConnection();
String sql = "select * " + "from tb_person where id=?";
PreparedStatement ps = null;
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.executeQuery();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <!-- An example log4j configuration xml file for log4jdbc -->
<!-- Logging levels are: -->
<!-- DEBUG < INFO < WARN < ERROR < FATAL --> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="stdout-appender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1}: %m%n"/>
</layout>
</appender> <appender name="sql-appender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/sql.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="-----> %d{yyyy-MM-dd HH:mm:ss.SSS} <%t> %m%n%n"/>
</layout>
</appender> <appender name="sql-timing-appender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/sqltiming.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n"/>
</layout>
</appender> <appender name="jdbc-appender" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/jdbc.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
</layout>
</appender> <appender name="jdbc-connection" class="org.apache.log4j.FileAppender">
<param name="File" value="./logs/connection.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
</layout>
</appender> <!--
The Following 5 logs can be turned on and off while the server is running
LIVE in order to trace the SQL and/or all JDBC coming out of the application. To turn a log on, set the level value to INFO or DEBUG (to see class name and
line number information in the log) The DEBUG setting is much more inefficient
but the output is much more useful. To turn off JDBC logging completely, you must set all 5 logs to a level higher
than ERROR (FATAL is suggested.)
--> <!-- log SQL (pre-execution) plus exceptions caused by SQL -->
<logger name="jdbc.sqlonly" additivity="false">
<level value="debug"/>
<appender-ref ref="sql-appender"/>
</logger> <!-- log SQL with timing information, post execution -->
<logger name="jdbc.sqltiming" additivity="false">
<level value="fatal"/>
<appender-ref ref="sql-timing-appender"/>
</logger> <!-- only use the two logs below to trace ALL JDBC information,
NOTE: This can be very voluminous! --> <!-- log all jdbc calls except ResultSet calls -->
<logger name="jdbc.audit" additivity="false">
<level value="fatal"/>
<appender-ref ref="jdbc-appender"/>
</logger> <!-- log the jdbc ResultSet calls -->
<logger name="jdbc.resultset" additivity="false">
<level value="fatal"/>
<appender-ref ref="jdbc-appender"/>
</logger> <!-- log connection open/close events and dump of all open connection numbers -->
<logger name="jdbc.connection" additivity="false">
<level value="fatal"/>
<appender-ref ref="connection-appender"/>
</logger> <!-- this log is for internal debugging of log4jdbc, itself -->
<!-- debug logging for log4jdbc itself -->
<logger name="log4jdbc.debug" additivity="false">
<level value="debug"/>
<appender-ref ref="stdout-appender"/>
</logger> <!-- by default, log everything to the console with a level of WARN or higher -->
<root>
<level value="warn"/>
<appender-ref ref="stdout-appender"/>
</root>
</log4j:configuration>
Console输出:
log4j:ERROR No appender named [connection-appender] could be found.
2015-09-22 22:40:38.933 DEBUG debug: ... log4jdbc initializing ...
2015-09-22 22:40:38.934 DEBUG debug: log4jdbc.properties not found on classpath
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.debug.stack.prefix is not defined
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.sqltiming.warn.threshold is not defined
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.sqltiming.error.threshold is not defined
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.booleanastruefalse is not defined (using default value false)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.sql.maxlinelength is not defined (using default of 90)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.fulldebugstacktrace is not defined (using default value false)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.statement.warn is not defined (using default value false)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.sql.select is not defined (using default value true)
2015-09-22 22:40:38.934 DEBUG debug: x log4jdbc.dump.sql.insert is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.update is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.delete is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.create is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.dump.sql.addsemicolon is not defined (using default value false)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.auto.load.popular.drivers is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.trim.sql is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.trim.sql.extrablanklines is not defined (using default value true)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.suppress.generated.keys.exception is not defined (using default value false)
2015-09-22 22:40:38.935 DEBUG debug: x log4jdbc.drivers is not defined
2015-09-22 22:40:38.942 DEBUG debug: FOUND DRIVER com.mysql.jdbc.Driver
2015-09-22 22:40:38.945 DEBUG debug: ... log4jdbc initialized! ...
工程目录下logs/sql.log
-----> 2015-09-22 22:40:39.241 <main> com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:379)
2. select * from tb_person where id=1
http://blog.csdn.net/alex1314159/article/details/47185365
http://sourceforge.net/projects/c3p0/?source=typ_redirect
https://github.com/arthurblake/log4jdbc
Log4jdbc demo的更多相关文章
- 通过一个demo了解Redux
TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...
- 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo
有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...
- 在线浏览PDF之PDF.JS (附demo)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...
- vue双向数据绑定原理探究(附demo)
昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...
- Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...
- iOS之ProtocolBuffer搭建和示例demo
这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...
- 钉钉开放平台demo调试异常问题解决:hostname in certificate didn't match
今天研究钉钉的开放平台,结果一个demo整了半天,这帮助系统写的也很难懂.遇到两个问题: 1.首先是执行demo时报unable to find valid certification path to ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
随机推荐
- 排列组合相关算法 python
获取指定长度得全部序列 通过事件来表述这个序列,即n重伯努利实验(二项分布)的全部可能结果.比如时间a表示为: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 假设每次实验为从 ...
- 【安卓笔记】高速的发展设置界面-----PreferenceActivity
通常app都会有一个设置界面,例如以下: 通常做法是自定义布局,然后在代码里面加入响应函数,并将结果保存到Sharedpreferences中. android给我们提供了PreferenceActi ...
- log4j 日志大小限制 分成30一个 不按日期分日志 按大小分成 按生产日期
首先说说生成按日期.不解释,大家都懂的,这种方法的缺点是很吃硬盘空间 log4j.rootLogger=INFO,logfile,stdout log4j.logger.java.sql=DEBUG, ...
- 使用QT 4.8.6 + Cmake 3.0.0 编译 最新版本OpenCv3.0.0
mingw32 (x32) gcc g++ qt opencv- -- cmake -rc1 windows x64 参考文章: http://blog.csdn.net/qiurisuixiang/ ...
- effective c++ 条款6 如果不想要就要告诉大家
这个条款应该和第五个放在一起 编译器默认生成 1 无参构造函数, 2 析构函数 3 拷贝构造函数 4 赋值预算符 当我们不需要,就要显示的告诉大家我们不需要. 方法:自己声明这个函数为private, ...
- String类疑虑
Java String类疑虑: 创 拼接 String是java的经常使用类之中的一个,本质是字符数组char[].String类是final类,不可被继承. 关于String的创建,能够通过new来 ...
- 认识Backbone (三)
Backbone.Collection(集合) collection是model对象的一个有序的组合,我们可以在集合上绑定 "change" 事件,从而当集合中的模型发生变化时f ...
- GitLab 之 Linux十分钟快装(转)
先把 Shell 命令贴出来,楼主以 CentOS release 6.5 (Final) 64位 为例: //配置系统防火墙,把HTTP和SSH端口开放. sudo yum install curl ...
- SDUTOJ 1298 活动选择
#include<iostream> #include<memory.h> using namespace std; int a[105],b[105],c[105],d[10 ...
- 【Java基础】“数三退一”问题的代码实现
现在有500个小孩,编号为0-499,手牵手连成一个圈,从第一个小孩开始"123123..."报数,数到三的小孩退出,求剩下的最后一个小孩的编号. public static vo ...