Motivation
One of the first lines a programmer will write in a new language is surely “Hello World”. In Java you can write to the console or to the error stream quite easy with a simple System.out.println("Hello World")
or System.err.println("Hello Error")
. Great! When the code grows, bugs creep into the code and make live a bit harder. At this point programmers should definately start a deep and loving relationship with the debugger that is delivered with the IDE instead of using System.out/err.println()
as debug method. Nevertheless – there are at cases, where a Debugger cannot (or hardly) be applied:
- The code runs in the IDE but not if startet directly. – What the hell’s going wrong?
- Handling of exceptions. An exception indicates a state that should not have happened and therefore it might be considered to be logged.
- The code is deployed to someone else and you cannot attach the debugger to his/her machine.
At either point, beginners tend to use System.out/err.println()
to trace the execution path. While this might be okay if the onlyone that is using the code is the programmer alone, this can be very annoying if you are working in a team: If you forget to remove the debug messages, you’re polluting someone elses console output. Even worse: if the code is deployed to a client which reports an error, you cannot raise/lower debug levels or just enable/disable debugging. Do you really want to send a “debug version”? (No you wouldn’t.)
Continue reading An introduction to the Logging framework (a.k.a. System.out.println is evil)