-1

I have made log4j appender that stores all logs into one file:

log.info("Hello world");

will store Hello World into the file. How I can make that even Exceptions are stored into that file as log. For example statement:

log.info(5 / 0);

should store whole stack from DivisionByZero Exception into file that is stated in log4j appender.

P.S. I know that I can put log.info(e.printStackTrace()) into catch clause, but I want every catched and non-catched exception is logged. I am testing, so there are many exceptions that shouldn't be catched, just thrown. I need configuration, property, or any other statement related to log4j that will stream all exceptions into its appender.

P.S.S. I know I can redirect stderr output to file, but I don't want touch the console.

milosdju
  • 783
  • 12
  • 27

1 Answers1

2

If you want exceptions you are already catching to be logged, you have to make sure that your catch block performs the logging, like:

catch (WhateverException e) {
  log(e); // just a place holder ...
}

If you want to log uncaught exceptions then you need an uncaught exception handler. See here for an example.

It's important to make sure that each and every exception gets caught at some point if you want the ability to log it.

And just to be clear: it should be possible to use an uncaught handler that simply logs and re throws (worst case you have to wrap the incoming throwable into a RuntimeException).

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I am testing, so there are many exceptions that shouldn't be catched, just thrown, that's why I put P.S. I need configuration, property, any statement related to log4j that will stream all exceptions into its appender. – milosdju May 31 '17 at 12:14
  • 1
    As explained: **logging** can only happen in catch blocks. If you want all exceptions to be logged, you **have** to catch them *somehow*. That is what this uncaught handler exists for! – GhostCat May 31 '17 at 12:18
  • For 99% of problems in programming there is always some solution, so if you think there is no solution on some problem, better be quiet and wait for other to answer it. I have stated that I can't **catch** exceptions, on whatever ways you are talking, so it's not the solution. And by the way, I have used that feature that I want to implement earlier in some framework, and it wasn't implemented in your fashion, – milosdju May 31 '17 at 12:27
  • 1
    @milosdju I am simply explaining you what is possible in the Java language. It is really not my problem if you find those restrictions annoying. I didn't make them up. And I would be *really* surprised if you will hear anything else. I *know* a little bit about java. And then: your last sentence simply doesnt make any sense. When you have code that doesnt do what you want, edit your question and add a [mcve]. Claiming that you have code that doesn't work is not a statement we could help with. – GhostCat May 31 '17 at 12:38
  • Please see this for an example of how @GhostCat knows a bit about your specific problem area : https://stackoverflow.com/questions/42942952/using-try-catch-for-preventing-app-from-crashes/42942998#42942998 – Sam May 31 '17 at 12:41
  • @GhostCat you don't understand. I put the problem not code that need to be fixed. Mentioned framework has working implementation on this issue, unfortunately I have no access to it right now. Thanks anyway! – milosdju May 31 '17 at 13:06