Hey.
There is a method that throws an exception:
throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException{ How to log these exceptions to a log using log4j?
Hey.
There is a method that throws an exception:
throws ClientProtocolException, ServerException, UnsecuredConnectionAttemptError, IOException{ How to log these exceptions to a log using log4j?
Intercept it in catch and write to the log with the level you need.
// в начале класса private static final Logger logger = LogManager.getLogger(<имя класса>.class); try { // код } catch(ClientProtocolException | ServerException | UnsecuredConnectionAttemptError | IOException ex) { logger.error("Что-то пошло не так", ex); throw ex; } If you are actively using threads, then it is possible to suspend the exception handler on the stream. Then, if an exception occurs during operation and it is not processed, it will be possible to intercept it and do some useful work, for example, to make an entry in the log.
It looks like this:
Thread thread = new Thread(() -> { method(); }); thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { //логирование сообщений об ошибках } }); thread.start(); private static void method(){ // что то там кидается } Source: https://ru.stackoverflow.com/questions/656106/
All Articles