2

We are maintaining two different log files, one to log only error messages, and other for debug information. I don't want to clutter the error file with exception stack trace, so I have to add following two lines each time on catching any exception

try{
  ...
}
catch(Exception e){
   log.error("Error during SOME_SERVICE {}" , e.getMessage());
   log.debug("Exception ", e);
}

Is there any simpler way to do this, may be in a single line of code?

sidgate
  • 14,650
  • 11
  • 68
  • 119
  • You can try this log.error("Error during SOME_SERVICE {}" , e);. If logger level is info then it will print error message, otherwise if logger level is debug then it will print entire trace – Siva Kumar Sep 29 '15 at 05:56
  • 1
    While using one line instead of two lines would strictly speaking save 50% more space, it's hardly an issue. A bigger issue is why would you not log the full stack trace when encountering an error? It's not exactly "clutter", it's what the log files are meant for. As for your original question, I suspect you could do some trickery with the logging frameworks to print the stack trace only when debug level is on, but for that you'll need to interact with the loggers (and write many lines of code). – Kayaman Sep 29 '15 at 06:25
  • @Kayaman error loggers are just for system admins and debugs are actually for support enggs. Your suggestion makes sense, I either have to live with it, or define my own Logger – sidgate Sep 29 '15 at 09:46

1 Answers1

0

As suggested in the comments, I have two options, either live with it, or write my own custom logger that would do the expected behavior. For now, I leave it as is.

For custom logging there are tons of resources, so won't post the solution here.

Implement Custom Logger with slf4j

Community
  • 1
  • 1
sidgate
  • 14,650
  • 11
  • 68
  • 119