我想将 log4j 用于 java 和 sql。我想在课堂上不使用“logger.debug("log4j logger")”来打印 java 和 sql 日志。
下面是使用的类和 log4j 属性
package com.log4j;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LogTest {
public static void main(String[] args) throws Exception{
final Logger logger = Logger.getLogger(LogTest.class);
PropertyConfigurator.configure("log4j.properties");
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection("XXX", "YYY", "ZZZ");
String inserQuery = "insert into table1 (name,id) values (?,?)";
PreparedStatement prestat = conn.prepareStatement(inserQuery);
prestat.setString(1, "Test");
prestat.setString(2, "2");
prestat.executeUpdate();
}
catch (Exception ex)
{
System.out.println("Exception: " + ex.getMessage() + "");
}
}
}
以下是 log4j 属性
log4j.rootLogger=DEBUG,CA
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
请指教。
任何帮助将不胜感激
谢谢