[英]Java java.util.logging Strange behaviour of the logger in combination with HSQLDB
情况:我有一个静态数据库类,使用HSQLDB,并且我想在其中也使用日志记录。
我设置了记录器,设置了所有语句,但是在使用特定方法之后,记录器似乎不再起作用。 没有异常抛出,似乎已经死了。
这是Logger类:
public class GLogger {
private static GFormatter formatterHTML;
private static FileHandler fileHTML;
private static boolean isReady = false;
public static void setup() throws IOException {
Logger logger = Logger.getLogger("");
fileHTML = new FileHandler("conf/logging.html");
formatterHTML = new GFormatter();
fileHTML.setFormatter(formatterHTML);
logger.addHandler(fileHTML);
isReady = true;
}
public static boolean isReady() {
return isReady;
}
}
这是Database类的(一部分):
public class Database {
private static final Logger LOG = Logger.getLogger(Database.class.getName());
/**
* The database server class
*/
private static Server hsqlServer;
/**
* The connection class. Used to manage request to the database.
*/
private static Connection connection;
/**
* The name of the database
*/
private static String dbName;
public Database(String databaseName, String fileName) {
LOG.setLevel(Level.FINEST);
hsqlServer = null;
connection = null;
dbName = databaseName;
// ###### LOGGER ######
LOG.finest("Creating a new database server class");
// ####################
hsqlServer = new Server();
hsqlServer.setLogWriter(null);
hsqlServer.setSilent(true);
hsqlServer.setDatabaseName(0, dbName);
hsqlServer.setDatabasePath(0, "file:db/" + fileName);
}
/**
* Start the new database class.
*/
public static void start() {
// ###### LOGGER ######
LOG.finest("Starting a new database class");
// ####################
hsqlServer.start();
connection = null;
}
/**
* Start the new connection to the specified database, using the default username "sa" e no password
*/
public static void connect() {
try {
// ###### LOGGER ######
LOG.finest("Getting a connection to the newly started database");
// ####################
Class.forName("org.hsqldb.jdbcDriver");
// Default user of the HSQLDB is 'sa' with an empty password
connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/garby", "sa", "");
} catch (ClassNotFoundException e) {
// ###### LOGGER ######
LOG.severe("Impossible to connect to the database. ClassNotFound");
// ####################
} catch (SQLException e) {
// ###### LOGGER ######
LOG.finest("Impossible to connect to the database. " + e.getCause());
// ####################
}
}
// ... more other stuff, not important and then the main method:
public static void main(String[] args) {
if (!GLogger.isReady()) {
try {
GLogger.setup();
} catch (Exception e) {
}
}
new Database("garby", "garbydb");
Database.start();
Database.connect();
Database.tableStructureCreation();
Database.populateFromFile("conf/populateServ.txt", "services");
System.out.println(Database.listServices());
Database.stop();
}
我删除了与问题无关的所有方法和其他操作。
如果我运行该类,则一切正常(!!数据库添加了正确的结构和数据,并在第二行“ system.out”中进行了验证)。 不起作用的东西应该是记录器。
如果我打开html文件(为简单起见,我省略了htmlFormatter,这没什么用,它只格式化日志消息),我只能读取两行,在构造函数中一行,在start()方法中一行。
经过几次尝试,我了解到问题出在
hsqlServer.start();
在公共静态void start();中 方法。
实际上,如果在主体上将Database.connect()移到Database.start()之前,我可以读取4条消息(1条来自构造函数,2条来自连接[得到conn...。 ..]和1开始),然后什么也没有。
如果我在[hsqlServer.start()]行中添加注释,则可以读取所有日志消息(显然,由于缺少该语句,直到某处引发NullPointerException为止)。
我真的不知道为什么以及如何解决这个问题。 这件事阻塞了我所有的程序Log消息,因为显然“ Database”类几乎先于其他任何类启动,并阻塞了所有Log消息。
有任何想法吗? 先感谢您
[再次,对不起我的英语不好,我希望我可以理解]编辑:我也删除了Javadoc注释-没用。
如果存在,则HSQLDB将使用java.util.logging或Log4J。 它重新配置了日志记录设置,以使日志记录正常工作。 如果要自己配置日志记录,则必须包括系统属性设置hsqldb.reconfig_logging = false。 然后,您需要配置要包含HSQLDB日志消息的级别。 请参阅指南:
http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_jdc_logging
启动服务器之前,设置系统属性的一种方法是:
System.setProperty("hsqldb.reconfig_logging", "false");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.