Commons-logging provides a general-purpose interface and various logs to achieve engagement. I used to use log4j, so I want to change the log4j code to commons-logging, I have to do an article and I have to do it. Classification: public class jdbcmap extends basemap {
/ ** * All logging goes through this logger * / private static log log = logfactory.getlog (jdbcmap.class);
private Statement sta = null; / ** * obtained by the current session session connection * @return have connection * / public Connection getConnection () throws HibernateException {return getSession () connection ();.} public Statement getStatement () throws HibernateException, SQLException {If (sta == null) {sta = getsession (). Connection (). Createstatement ();} return sta;} public void closestatement () THROWS SQLEXCEPTION {if (sta! = Null) {sta.close (); sta = null;}} / ** * * @param execute a query SQL query * sql * @return * @throws HibernateException if can not get connection from session * @throws SQLException if can not executequery from connection * / public ResultSet executeQuery (String SQL) throws HibernateException, SQLEXCEPTION {IF (Log.IndebuGenabled ()) {log.debug ("debug execute query: sql);} if (log.isinfoenabled ()) {log.info (" Info Execute Query) " SQL);
Statement Sta = getStatement (); ResultSet res = sta.executeQuery (SQL); Return Res;} public static void main (String [] args) {if (log.isdebugebush ()) {log.debug ("Debug Execute Query: ");}} F (log.isinfoenabled ()) {log.info (" info execute query: ");}}}
I built commons-logging.properties and log4j.properties in the web-inf / classses directory, I built two appenders, commit-logging.properties content: org.apache.commons.logging.log = org.apache.commons .logging.impl.Log4JLoggerlog4j.properties content: log4j.rootLogger = debug, dest1, dest2log4j.appender.dest1 = org.apache.log4j.ConsoleAppenderlog4j.appender.dest1.layout = org.apache.log4j.PatternLayoutlog4j.appender.dest1. Layout.conversionPattern =% D% -5P% -5c {3}% x ->% M% N
log4j.appender.dest2 = org.apache.log4j.RollingFileAppenderlog4j.appender.dest2.File = c: //bridge.log log4j.appender.dest2.MaxFileSize = 100KB # Keep one backup filelog4j.appender.dest2.MaxBackupIndex = 3
Log4j.Appender.Dest2.Layout = org.apache.log4j.patternLayoutLog4j.Appender.Dest2.Layout.conversionPattern =% D [% T]% -5p% -5c {3} (% L)% x ->% m% n and copied commons-logging.jar and log4j.jar to the web-inf / lib directory, all preparations have been done, and I built a servlet, called JDBCMAP MAP = New JDBCMAP (); map.executeQuery (SQL); but after running the servlet, only this sentence in the console log.info; c: //bridge.log This file is not established, so it is concluded Commons-logging. Did not call log4j, but call JDKLog (Commons-logging default Log4j first, I can't find JDK1.4log, or simplelog), then I will run jdbcmap directly (see main function), and I got me If you have the result, C: //bridge.log is established, the content and console are played, log.debug ("Debug Execute Query:") This sentence has also been played out. Finally, it is a bit of a little, Let me help me see it, it is best to try it, I know that I will tell me, I suspect that it is not ClassLoader problem.