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:
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:
You can always switch back to the default setting using:
Java:
Configuration.setOut(System.out)