Situation:
I have a static Database class, using HSQLDB and I want to use Logging also in it.
I set up the logger, set up all the statements, but after using a specific method, the logger seems not to be working anymore. No exception are thrown, just seems to be dead.
this is the Logger class:
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;
}
}
This is the (part of) Database class:
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();
}
I deleted all methods and other action that are not related to my problem.
If I run the class, all works well (!! the database add the correct structure and data, verified with second last line “system.out”). The thing that don’t work how should is the logger.
If I open the html file (I omitted the htmlFormatter for simplicity, that’s not useful, it only format the log message), I can read ONLY two lines, the one in the constructor and the one in start() method.
After few tries, I understood that the problem is in the line
hsqlServer.start();
in public static void start(); method.
In fact, if in the main I move the Database.connect() BEFORE the Database.start(), I can read 4 messages (1 from the constructor, 2 from the connect [getting a conn…. impossible to conn…] and 1 from the start) and then nothing.
If I comment that line [hsqlServer.start()] I can read ALL log messages (obviously until somewhere is thrown a NullPointerException due to the missing statement).
I really can’t figure out why and how to solve this problem. This thing is blocking me all my program Log messages, because obviously the “Database” class is started almost before any other and blocks all Log message.
Any ideas? Thank you in advance
[again, sorry for my bad english, I hope I can be understandable]
EDIT: I removed also Javadoc comment – not useful.
HSQLDB will use java.util.logging or Log4J if either is present. It reconfigures the logging setup for logging to work properly. If you are configuring logging yourself, you must include the system property setting hsqldb.reconfig_logging=false. You then need to configure the level at which you want HSQLDB log message to be included. See the Guide:
http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_jdc_logging
One way of setting the system property before you start the Server is this: