Customizing The Debug Message Output

Debug Messaging System topic explains how to activate the debug messages in your application. However the default console output has very limited possibilities and can only be used effectively in console applications. To use debug messaging system in any environment db4o gives you an API to redirect debug output to another stream:

 

Java: 

Configuration.setOut(java.io.PrintStream)

An example below shows how to create and use a log file for debug messages:

DebugExample.java: setCarsWithFileOutput
01public static void setCarsWithFileOutput() throws FileNotFoundException 02 { 03 // Create StreamWriter for a file 04 FileOutputStream fos = new FileOutputStream("Debug.txt"); 05 PrintStream debugWriter = new PrintStream(fos); 06 07 // Redirect debug output to the specified writer 08 Db4o.configure().setOut(debugWriter); 09 10 // Set the debug message levet to the maximum 11 Db4o.configure().messageLevel(3); 12 13 // Do some db4o operations 14 new File(YAPFILENAME).delete(); 15 ObjectContainer db=Db4o.openFile(YAPFILENAME); 16 try { 17 Car car1 = new Car("BMW"); 18 db.set(car1); 19 Car car2 = new Car("Ferrari"); 20 db.set(car2); 21 db.deactivate(car1,2); 22 Query query = db.query(); 23 query.constrain(Car.class); 24 ObjectSet results = query.execute(); 25 listResult(results); 26 } finally { 27 db.close(); 28 debugWriter.close(); 29 } 30 Db4o.configure().messageLevel(0); 31 }

Using a log file for debug messages has several advantages:

  • debug information is available after the application has terminated;
  • console output is not polluted with debug messages;
  • debug information from the clients can be available on the server.

You can always switch back to the default setting using:

Java: 

Configuration.setOut(System.out)