This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
hello good people of the java ranch,
Since some of you did such a great job of answering a question i had some days back i decided to chance another.
I have little to no experience with logging outside of going over some simple 1 class examples from various online sources so today I've taken a stab at using java logging in some code i'm working on.
The program consists of 3 classes which is why i felt inspired - i wanted to see logging working across more than one class. So on i went and gave it a whirl.
The way i've went about handling the logging in the main class is like this......
The console output is removed, the logging is saved to the file "LogFile.log" and the world keeps turning.
In the other classes i've handled the logging like this .......
And like this.........
When i compile and run this everthing runs, i get one "LogFile.log" file in the directory specified, the log file shows the sequential logging from all 3 classes and there's no compiler warnings or strange behaviour that i've notice.
However....... i'm not convinced. Have you ever had the feeling that even though something is working fine and showing no signs whatsoever of an imminent catastrophe lurking underneath the cool collected exterior it will come as no surprise when the roof comes crashing down? I'm pretty certain there's things going on here unbeknownst to me that amount to an accident waiting to happen and that the i've probably broken all sorts of rules that will result in the universe imploding.
Which is where i guess someone else with a better understanding than me can step in if they could be so good and point out what is not right in the approach i've taken.
The logger isn't going to log anything you don't explicitly tell it to. If you check the Javadoc, you'll see it has a number of different methods for outputting messages at different logging levels. If you don't explicitly tell it to log an exception or whatever, you won't see it in the log.
Joined: Nov 05, 2009
Yup, that much i know. The logging is working fine in this code, all classes will output log messages they encounter to the same file in sequence. I have logging messages sprinkled throughout the code and i can adjust the log levels for each class individually. It's all working fine.
But it's all working TOO fine. I just have the feeling there is something flawed in how i've implemented the way all loggers output to the same file. There's nothing i can point to and say "There..." because as i said originally i have little to no experience with logging. It's probably because it was too easy for me to get it working on a first attempt.
Past experience has taught me to be very suspicious of anything that works too easy. This may have been confounded by the fact that any of the logging tutorials or online sources i've had a go at digesting seemed to be either overly complicated or contradictory to each other. Couple this with the fact that more people are giving and receiving advice on alternative logging packages like log4j than on the java standard logging utils and you end up in .
I don't know, maybe i'm just being overly cautious. I hope i'm not wasting people's time with something frivilous.
Mike: use log4j.
Its open source, well defined, etc.
I don't know why the java.util.logging stuff was included, it replicates code in several other logging frameworks, is no faster, no cheaper and is not as complete.
When you use log4j's logging, you can change nearly everything at runtime, including things like logging to a database, across a network, etc. in addition to the usual log to a file someplace.
John de Michele
Joined: Mar 09, 2009
I've used the JDK logging package quite a bit, and I agree that the documentation is a little opaque (but the same can be said for log4j, which I've also used). I think this may be a case where that paranoia instinct is getting a false positive. As Pat said, you can try log4j. You might also want to look into the slf4j package. It's a facade, so it can work with a number of different logging packages, including log4j and the JDK logging package.
John de Michele wrote: look into the slf4j package.
I used it for a while, it works fine. But after a bit, I decided that what log4j did was sufficient and so the ability to change it under the facade of the slf4j package lost its appeal.
Plus, I'm using a lot of other packages, such as the Apache HTTP client code, the Google Collections code, Bear's FrontMan, etc. and they all use log4j. So once you get over the learning curve, its all easy.
It really comes down to a personal question for one man efforts, or what the team uses for group efforts.
I'm starting to get the feeling that there's a general dislike for the java logging utilities.
I'll definitely have to check out log4j and a few of these others that were mentioned but at the moment i'm gonna keep banging away on java.util.logging until i have a firm understanding of the mechanisms, i'm guessing from the comments when it comes to databases and logging across networks it'll be time to move on to something more versatile.
I'm gonna take it that my logging efforts in the above code were not so obviously flawed as i thought then. Thanks again guys for your replies, comments and suggestions, they are most appreciated, no doubt i'll be back sometime soon with another irrelevant question.
Happy new year ladies and gentlemen.
John de Michele
Joined: Mar 09, 2009
I got exposed to slf4j as a side-effect of using Hibernate for a project at home. I agree that for something like that, it's probably more than necessary.
You can count me as one of the rare people who doesn't have a problem with the JDK logging. That was the logger that was used when I learned Java. It does have one benefit: it's included with the JDK, so it will always be there. Other than that, it's not really special, but then again, I'm not sure that logging needs to be special .