Answer the question
In order to leave comments, you need to log in
How to improve application architecture?
I am learning Spring, I want to move error processing and logging into a separate class: there are a number of actions (in different controller classes), the results of which are logged - whether they were successful or not. Currently it's done like this:
- a base class that performs operations. if the operation ended unsuccessfully, then an exception is generated with the text of the error message, otherwise it is displayed in the log that the operation was successful
if (item==null) {
throw new MyException(Message.ERROR_READING);
}
else
{
log.info(Message.SUCCESS_READING);
}
public final class Message {
public static final String ERROR_READING = "Ошибка получения данных";
public static final String SUCCESS_READING = "Данные получены";
}
public class ItemException extends RuntimeException {
static Logger log = Logger.getLogger(App.class.getName());
public MyException(String message) {
super(message);
log.error (message);
}
}
Answer the question
In order to leave comments, you need to log in
1) And what prevents immediately on the spot to log an error without throwing an exception? Is he really needed there?
3) Do not log from the exception constructor. Not everyone will be able to guess to find the logger call there.
2) There is a good thing for messages - Resource Bundle. They allow at the same time to make internationalization not as difficult as it could be.
What's wrong with the standard try/catch?
If you really want to take everything out - make a banal ExceptionHandler and drop the caught and created exceptions into it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question