I'm starting my app on Linux server with a command like this
/usr/local/bin/jsvc \
-home /usr/local/jdk1.8.0_111 \
-cp /opt/myapp/myapp.jar:/opt/myapp/lib/* \
-user myappuser \
-outfile /opt/myapp/out.log \
-errfile /opt/myapp/error.log \
-pidfile /opt/myapp/myapp.pid \
com.example.MyApp
I'm using log4j for logging in my app and it has own configuration described in log4j.properties which is located inside myapp.jar.
I want to be able to catch any exception which happens in the app and to send it to me via email. I have configured org.apache.log4j.net.SMTPAppender in log4j.properties for that purpose.
But currently RuntimeException, like NullPointerException are just printed to stderr which jsvc redirects to error.log without applying log4j formatting pattern.
Is it possible or should I run the app without jsvc wrapper like
nohup java -jar /opt/myapp/myapp.jar &
or is there better solution?
Answering myself. Just set default UncaughtExceptionHandler at the beginning of your code entry point
log4j.properties