Skip to main content

Preventing Java from printing the stack trace on certain exceptions



I have a security class that throws an AccessDeniedException on a user not being authorized for a certain operation. I made it even neat and put a msg in there with the info I need.





The problem is that now my logs are full of stack traces for every single time I throw. I don't care to see the stack trace for THIS exception, and the stack traces are bloating my logs.





Is there a way to tell Java not to print the stack trace to the logs if it's this exception?





Thanks!





--





llappall


Comments

  1. The problem is that now my logs are full of stack traces


    This is like saying "my fire alarm keeps going off, how can I make it quieter?". There are only two good courses of action.


    Determine that these are false alarms, and alter your alarm system so it is not so sensitive to events that are not actually dangerous. The throwing of an exception does not indicate a bug, or even anything very remarkable, if it is a checked exception. Even if a checked exception is worth logging, the stacktrace will not be worth logging, because a stacktrace is useful only for debugging.
    Determine that the alarms are genuine, in which case find out why fires keep breaking out and fix whatever is igniting them. That is, getting fixing the bugs that cause exceptions to be thrown. If you have many bugs that flood your log file it does not matter which you fix first, so just fix whichever are easiest to read in the log file

    ReplyDelete
  2. You need to investigate where you are catching (if catching) this exception, and ensure that you don't log the exception, but only the message...

    ReplyDelete
  3. Either you can decide to catch that particular exception and don't do anything about it.
    Either you can set the UncaughtExceptionHandler on the current Thread.

    Thread.currentThread().setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)


    and in the handle you perform an instanceof on the exception to see if it is that particular exception

    ReplyDelete
  4. I don't know if this will help you with your logging, but if you are creating an Exception purely to improve the logic and readability of your code, and if you never expect to wonder where the heck it came from, then override the fillInStackTrace() method, like so:

    public class AccessDeniedException extends Exception {
    public synchronized Throwable fillInStackTrace() { return this; }
    }


    This will improve your program's performance--dramatically if you throw a lot of them. It won't eliminate logging. The other answers should help with that, but if they don't your log messages will be much reduced.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex