I seem to be having a “tools fortnight” at the moment :), because we’ve been trying out both log4j and java.util.logging in the grexengine core libs. Now that we have a branch of the core libs which replaces our proprietary/closed logging system with log4j, and another for java.util.logging, we could do a direct comparison.
Which means I temporarily have an overview of the topic with equal knowledge of all the main options, and thought it might be handy to summarise that whilst it’s fresh, and that it would be handy for people here…and, as with my UML roundup, I expect that some of the Titans ;D on this forum will have some valuable extra points to add.
Sadly, my conclusions are mostly not good - however, we already HAVE a logging API that we’ve been using and extending for years; for people not using logging at the moment, I’d say it’s still a massive improvement to adopt one of the major ones despite their current inadequacies.
java.util.logging
Ohmygod. Sun, what happened with the documentation for Java 1.4? Was there an explosion in a magnet factory that sent magnets flying into all places where the data was backed up, and they were so hot that all the paper copies caught fire too? Or was there just one copy of the logging docs, kept in the same room as the NIO docs, and they both died in one tragic accident? You started so well in the 1990’s, with the JFC and it’s “trails” - did the authors and maintainers all leave for better paid jobs elsewhere? How come there’s no official Java Tutorial ™ trail on logging? Isn’t this important enough to be considered “fundamental” ?
Total time to understand enough of util.logging to do a “hello world” from scratch, without pasting code: about 20 minutes. Total time spent trying to load configuration from a file: 2 hours, and it still didn’t work.
We explored config files further, and found that Sun has almost no flexibility here. Their logging API is emasculated if you need flexibility or control over what file is loaded from where - and any error messages if it’s not found. Boom! Not good! The problem here is that with Sun, you MUST put your logging configuration file in ONE place: your JRE lib directory!
Think about that for a second. Sun wants you to have a single configuration file SHARED between EVERY SINGLE APP ON YOUR HARD DISK. Ahem. Can anyone see a teensy little problem? What happens the first time an app tries to install it’s own one, has a bug with not checking the existence first, and suddenly breaks logging in every other app you run? Wow. Brain-dead or what.
But that’s OK, you (eventually) realise - because Sun lets you choose which file to load. OK, so you have to load it via a System Property (no other choices - it HAS to be this way with Sun); and of course if you make a typo in the property you’ll get no warning or error message, which is one of the reasons why normal people don’t use properties for this kind of thing.
Except…Sun’s API is broken if you set the property in your source code. It simply doesn’t work properly, apparently because they do some stuff at startup (before your code to change the loaded filename gets executed) which we could never work out how to re-inoke. We couldn’t get around the following horror of Sun’s logging:
- If you load a named configuration from disk, it doesn’t work until you “reset” it
- Resetting a configuration trashes the loggers and does something nasty to the logging hierarchy so that lots of loggers cease to work from then on - we proved this by using a file that did nothing, and when commenting out the load line, it worked, and when you didn’t, it didn’t - although if you put anything in the file (like changing log levels) the changes were reflected in the few loggers that continued to work anyway.
The sad truth is that - apart from lethal bugs like this, and no documentation - the longer you dig in the Sun API the more you realise it wasn’t designed, it was hacked together (of course, the reason you are digging in it is because they didn’t write any docs, so you have to dig through the method names to try and find something - anything! - that might be related to your problems). I get the impression that some intern was given the job of making this, that they copied the design of log4j, and then because he/she was a beginner programmer, they produced a naive, buggy API that undermined several of it’s own design decisions (because they didn’t fully understand the purpose of some things they were cloning).
Where is the most basic functionality, like rotating logfiles? Log4j has this, but Sun’s doesn’t. Argh!
Eventually, we simply gave up entirely. Rating:
- Robustness: 1/10. If a logging API can’t even be configured without unexplained bugs, and you have no workaround documentation, it’s useless.
- Documentation: 3/10. Where is it? All you give is is javadocs. No good at all.
- Performance: ?. Didn’t even bother.