3

In certain situations, I'd like to catch an Exception but still show the Stack Trace in the stackTrace log defined via the Log4J properties in grails. How can I do that?

I know I could write it into the "normal" log using log.error myException.stackTrace for example, but I don't want the stack trace to fill up my normal log file.

Jörg Brenninkmeyer
  • 3,304
  • 2
  • 35
  • 50
  • i don't know Java (anymore) or Log4J (anymore) or Grails - but i would think you can do what's standard is any programming language. You could throw an exception and catch it. That way no exception will leak into your log. Unless you don't want to throw exceptions in non-exceptional situations - which i completely agree with - and you're asking how, in Java, to fetch the current stack frame. – Ian Boyd Dec 06 '10 at 16:11
  • I do catch the Exception. And I also want the Stack Trace to appear in the log, which is not the case if I catch it. – Jörg Brenninkmeyer Dec 06 '10 at 16:29

1 Answers1

3

there is already a stacktrace log generated by grails.

you can get that logger and then add your log statements in the catch block of the exception

LogManager.getLogger("StackTrace")

http://grails.org/doc/latest/guide/3.%20Configuration.html#3.1.2%20Logging

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • Yes, but the stack trace of a caught Exception does not appear there! How can I catch the Exception and still have the stack trace logged in the stacktrace log? – Jörg Brenninkmeyer Dec 06 '10 at 18:15
  • That's it - thank you! I can log the full Stacktrace using org.apache.log4j.LogManager.getLogger("StackTrace").error myMessage, myException – Jörg Brenninkmeyer Dec 06 '10 at 19:54