我在我的主类中创建了一个记录器:
public static Logger logger = Logger.getLogger( MainClass.class.getName());
我宣布它是公共静态的。
这是 log4j.properties:
根记录器选项
log4j.rootLogger=INFO, file
将日志消息定向到日志文件
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\loging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
现在我想在不同的类中访问这个记录器对象。当我尝试直接提供记录器时,尽管我将其设为公开静态,但它给出了错误。
下面是我试图从中访问记录器对象的类:
public class Db_Connector
{
MainClass.logger <------error
//create ResultSet Class Object
public ResultSet GetDataFromDB(){
ResultSet resultset = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connecting to the database...");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@host:1521:Db", "User","pass01");
Statement statement = connection.createStatement();
resultset = statement.executeQuery("select * from employee");
}
catch (Exception e) {
System.out.println("The exception raised is:" + e);
}
return resultset;
}
}