2 Replies Latest reply on Mar 19, 2012 2:59 PM by hocuspocus

    Looking for good Java source code examples of error handling and logging

    hocuspocus

      I'm trying to find some source code that illustrates proper Java coding techniques, particularly logging and error handling. A friend suggested I ask here as he thought your source code was particularly good and would give me excellent examples to imitate.

       

      I know very little about JBoss and it is clearly a very complex project with many different components at many different levels of completion. I have no idea how uniform the quality of code is and as a relative novice in Java, I wouldn't necessarily recognize good professional-quality code myself. Therefore, I  hope someone here can point me to a couple of projects within JBoss that are particularly well-written with regards to logging and error-handling. Also, if you can guide me on exactly how to find the source code for that project, I would greatly appreciate it.

       

      With a bit of luck, I might even bet more actively involved in a project or two myself in the not too distant future once I'm satisfied that my coding is better. (Of course, I'll also need to have a little more spare time too!).

       

      If you need any more information, please say so.

        • 1. Re: Looking for good Java source code examples of error handling and logging
          peterj

          Good example of logging and exception handling? Well, if you mean the ability to use logging to output stuff, including excpetion stack traces, I guess that fits. But if you mean providing meaningful information to users of the application server, then no. The developers apparently never took a writing class in school, or perhaps they weren't paying attention, or perhaps they thought that lessons learned in that class did not pertain to logging output. Whatever it was, they forgot a fundamental principle of writing: know your audience. Based on what i have seen as far as logging and exception error messages are concerned, they have no idea who their audience is. They think is is themselves. It really should be system administrators managing numerous application servers and developers trying to debug their own applications.

           

          As an example, look at the log messages that show up on the console when you start JBoss AS. Which of those messages are really of importance to a system administrator. Or even a programmer deploying their app? Very few. You end up having to sift through useless stuff to get at important stuff. And there is some important stuff that never gets logged (for example, the log used to tell you which port JNDI the app server was using, a useful piece of information, but that log message disappeared at one point.)

           

          Browse through the forums and you will find numerous examples of:

          a) False error messages. People post exception stack traces or other error messages and want to know what they did wrong, when the answer is nothing, it is a debug message, or a warning that can be ignored.

          b) Unfathomable error messages. People post exception stack traces that show up when deploying their application, or while running their application, and they have no idea what went wrong because the error message is written for theprogrammer who wrong the code that threw the exception. So the error message means something to someone on the JBoss AS team, but everyone else is clueless. (The deployment error report printed at the end of startup when there are deployment error is one of the most misunderstood output artifacts mainly because while it looks like a defintive reasource about deployment errors, it rarely, if ever, contains any useful information.)

           

          Sorry for the rant, but logging is one of those things that has bugged me a long time. Some open source projects do it better than others (Ant and Maven are examples - with a standard level of logging you get informative messages about the steps in your build), but most have the same issues.

           

          So if you want to do logging right, talk to your audience. Find out what they need and want. Log that information for them. If your developers want to use logging as a means to debug their own code, make sure that their logging output doesn't contaminate the logs presented to end users.

          • 2. Re: Looking for good Java source code examples of error handling and logging
            hocuspocus

            Actually, I don't mind your rant at all. I am looking for exactly the kind of logging you describe as being the right way to do logging: messages written for operators/administrators to aid them in error recovery. Someone told me that JBoss did its logging that way but it seems I was sent to the wrong place. Given that you've posted over 8,000 times ince 2005, I'm assuming you're very familiar with JBoss and speak with some authority on this subject ;-)

             

            Given the large number of projects - and presumably developers - working on JBoss, it seems likely that some projects are better written than others. Perhaps my colleague saw one of those and assumed all the logging was of the same high quality throughout JBoss. But rather than trying to track down what project he saw, I think I'm going to take your hint and look at Ant or Maven. I actually use Ant so that would be interesting to see. I've never actually used JBoss yet.

             

            Thanks very much for your help, Peter!