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
01private 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 Configuration configuration = Db4o.newConfiguration(); 09 configuration.setOut(debugWriter); 10 11 // Set the debug message levet to the maximum 12 configuration.messageLevel(3); 13 14 // Do some db4o operations 15 new File(DB4O_FILE_NAME).delete(); 16 ObjectContainer container=Db4o.openFile(configuration, DB4O_FILE_NAME); 17 try { 18 Car car1 = new Car("BMW"); 19 container.set(car1); 20 Car car2 = new Car("Ferrari"); 21 container.set(car2); 22 container.deactivate(car1,2); 23 Query query = container.query(); 24 query.constrain(Car.class); 25 ObjectSet results = query.execute(); 26 listResult(results); 27 } finally { 28 container.close(); 29 debugWriter.close(); 30 } 31 }

Using a log file for debug messages has several advantages:

You can always switch back to the default setting using:

Java: 

Configuration.setOut(System.out)