【log4jdbc】log4jdbc日志框架介绍_梁小坏Leo_新浪博客
配置方法
1.导入log4jdbcjar
JDK1.4 JDK1.5 选择 log4jdbc3-1.2.jar
JDK1.6 JDK1.7 选择 log4jdbc4-1.2.jar
2.导入SLF4J的jar
下载最新SLF4J
http://www.slf4j.org/download.html
下载之后将得到2个jar文件,导入项目classpath
3.修改你的项目的JDBC驱动
Driver Class
oracle.jdbc.driver.OracleDriver
oracle.jdbc.OracleDriver
com.sybase.jdbc2.jdbc.SybDriver
net.sourceforge.jtds.jdbc.Driver
com.microsoft.jdbc.sqlserver.SQLServerDriver
com.microsoft.sqlserver.jdbc.SQLServerDriver
weblogic.jdbc.sqlserver.SQLServerDriver
com.informix.jdbc.IfxDriver
org.apache.derby.jdbc.ClientDriver
org.apache.derby.jdbc.EmbeddedDriver
com.mysql.jdbc.Driver
org.postgresql.Driver
org.hsqldb.jdbcDriver
org.h2.Driver
4.修改JDBC连接URL
比如你的URL为
那么修改为
jdbc:log4jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase
5.设置日志Logger在log4jdbc里面,新添加了5个Logger。
logger
jdbc.sqlonly
jdbc.sqltiming
jdbc.audit
jdbc.resultset
jdbc.connection
这里给出log4j.xml和log4j.properties的例子
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/">
</log4j:configuration>
log4j.properties
!==============================================================================
! log4j.properties - An example configuration properties file for log4j.
!
! Logging levels are:
! DEBUG < INFO < WARN < ERROR < FATAL
!==============================================================================
! turn on the internal log4j debugging flag so we can see what it is doing
log4j.debug=true
!==============================================================================
! JDBC API layer call logging :
! INFO shows logging, DEBUG also shows where in code the jdbc calls were made,
! setting DEBUG to true might cause minor slow-down in some environments.
! If you experience too much slowness, use INFO instead.
! Log all JDBC calls except for ResultSet calls
log4j.logger.jdbc.audit=INFO,jdbc
log4j.additivity.jdbc.audit=false
! Log only JDBC calls to ResultSet objects
log4j.logger.jdbc.resultset=INFO,jdbc
log4j.additivity.jdbc.resultset=false
! Log only the SQL that is executed.
log4j.logger.jdbc.sqlonly=DEBUG,sql
log4j.additivity.jdbc.sqlonly=false
! Log timing information about the SQL that is executed.
log4j.logger.jdbc.sqltiming=DEBUG,sqltiming
log4j.additivity.jdbc.sqltiming=false
! Log connection open/close events and connection number dump
log4j.logger.jdbc.connection=FATAL,connection
log4j.additivity.jdbc.connection=false
! the appender used for the JDBC API layer call logging above, sql only
log4j.appender.sql=org.apache.log4j.FileAppender
log4j.appender.sql.File=./logs/sql.log
log4j.appender.sql.Append=false
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n
! the appender used for the JDBC API layer call logging above, sql timing
log4j.appender.sqltiming=org.apache.log4j.FileAppender
log4j.appender.sqltiming.File=./logs/sqltiming.log
log4j.appender.sqltiming.Append=false
log4j.appender.sqltiming.layout=org.apache.log4j.PatternLayout
log4j.appender.sqltiming.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n
! the appender used for the JDBC API layer call logging above
log4j.appender.jdbc=org.apache.log4j.FileAppender
log4j.appender.jdbc.File=./logs/jdbc.log
log4j.appender.jdbc.Append=false
log4j.appender.jdbc.layout=org.apache.log4j.PatternLayout
log4j.appender.jdbc.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n
! the appender used for the JDBC Connection open and close events
log4j.appender.connection=org.apache.log4j.FileAppender
log4j.appender.connection.File=./logs/connection.log
log4j.appender.connection.Append=false
log4j.appender.connection.layout=org.apache.log4j.PatternLayout
log4j.appender.connection.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n
6.更多细节以及设置,请参照主页
参照log4jdbc的文档,在log4jdbc.properties文件中配置参数。\
- log4jdbc.debug.stack.prefix,只显示含调用栈的包名的语句。
- log4jdbc.sqltiming.warn.threshold,执行时间阀值,单位为ms,将Log级别调为Warning,则只会打印执行较慢的语句。
- log4jdbc.dump.sql.select/insert/update/delete, 过滤掉某些类型的语句。
Gottox/socket.io-java-client · GitHub
Socket.IO-Client for Java
socket.io-java-client is an easy to use implementation of socket.io for Java.
It uses Weberknecht as transport backend, but it's easy to write your own transport. See description below. An XHR-Transport is included, too. But it's not functional in its current state.
The API is inspired by java-socket.io.client.
Features:
- transparent reconnecting - The API cares about re-establishing the connection to the server when the transport is interrupted.
- easy to use API - implement an interface, instantiate a class - you're done.
- output buffer - send data while the transport is still connecting. No problem, socket.io-java-client handles that.
- meaningful exceptions - If something goes wrong, SocketIO tries to throw meaningful exceptions with hints for fixing.
Status: Connecting with Websocket is production ready. XHR is in beta.
How to use
Using socket.io-java-client is quite simple. But lets see:
Checkout and compile the project:
git clone git://github.com/Gottox/socket.io-java-client.git
cd socket.io-java-client
ant jar
mv jar/socketio.jar /path/to/your/libs/project
If you're using ant, change your build.xml to include socketio.jar. If you're eclipse, add the jar to your project buildpath.
Afterwards, you'll be able to use this library:
SocketIO socket = new SocketIO("http://127.0.0.1:3001/");
socket.connect(new IOCallback() {
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
}
@Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
@Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
@Override
public void onConnect() {
System.out.println("Connection established");
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
}
});
// This line is cached until the connection is establisched.
socket.send("Hello Server!");
For further informations, read the Javadoc.
Checkout
-
with git
git clone git://github.com/Gottox/socket.io-java-client.git -
with mercurial
hg clone https://bitbucket.org/Gottox/socket.io-java-client
Both repositories are synchronized and up to date.
Building
to build a jar-file:
cd $PATH_TO_SOCKETIO_JAVA ant jar ls jar/socketio.jar
You'll find the socket.io-jar in jar/socketio.jar
Bugs
Please report any bugs feature requests to the Github issue tracker
Frameworks
This Library was designed with portability in mind.
- Android is fully supported.
- JRE is fully supported.
- GWT does not work at the moment, but a port would be possible.
- Java ME does not work at the moment, but a port would be possible.
- ... is there anything else out there?
Testing
There comes a JUnit test suite with socket.io-java-client. Currently it's tested with Eclipse.
You need node installed in PATH.
- open the project with eclipse
- open tests/io.socket/AllTests.java
- run it as JUnit4 test.
