• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

log4j and exceptions

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If we use log4j as the standard mechanism for logging in our program, it works good. But when it comes to exception, specially unhandled exception, the default behaviour is that, the exception would be printed to the standard error.

So, what happens is that, let say I have defined one (or more if required) logger at the application level, and doing the entire logging through this logger only, still if an unhandled exception comes at runtime, that would not go to the log file being generated through log4j. To trace this unhandled exception, I will have to redirect the application's standard error to a different log file and keep checking this log file apart from the one generated through log4j.

Have anyone experienced this already? Is there any workaround for this?
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand your problem... Exceptions are a mechanism by which normal program flow is changed under exceptional circumstances during program execution. I'm guesing that your problem lies with RuntimeExceptions because they're not required to be caught, and the default behaviour of the JVM will be to pass them all the way up to the top level, be that a J2EE container or the command line for J2SE.

In a nutshell, if you want them logging, you need to catch them and log them somewhere, which from what you've written, I don't think you are doing. As to where to catch and log them, that's a whole different ball-park, and depends very much on the design of your program.

Does any of that help?

Regards,
 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I understand your point Daniel, but there's something more.

Let me further elaborate what I am trying to find out.

Let say I have a module containing around 15 to 20 java classes and I have used log4j for all the logging. No System.out.printlns. In my code, I have caught the Checked Exceptions wherever required and have done error reporting from there using the same logger.

So now, I will expect that everything will go to just one log file (one with log4j FileAppender). But, if a runtime exception occurs, that will go to the default system error. That will not go to the log4j log file, which I am going to keep checking for admin activities.

Now RuntimeExceptions are something that ideally should not arise, but in some worst cases, they may arise, and in in my view, catching RuntimeExceptions in the code is not a good practice generally. So now, I have to redirect the system error to a different file from the command prompt where I am going to run my application, and if a uncaught RuntimeException occurs java will print it's stack trace to the default system error.

Now as part of admin activity, I will need to keep checking both the log4j log file and the error log file to which I am redirecting from the command prompt. And I am trying to find out if there is a work around for this - is it somehow possible to redirect the trace of uncaught RuntimeExceptions in the same file as log4j rather than manually redirecting it to a different file and keep checking two files?

Hope this helps clarify the point.
 
Sheriff
Posts: 28400
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have to agree with Daniel:

In a nutshell, if you want them logging, you need to catch them and log them somewhere

There's no magic way to tell the JVM that uncaught errors should be logged into log4j. You would have to write some code to do that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic