0
votes

I have a spring-boot application set-upped including logging using log4j. In the application, there are few layers such as controller, service, models, repositories, exception etc. At the moment, i have included ERROR level logs in the exception layer.

@Component
public class ExceptionFactory {

    private static final Logger LOG =   LoggerFactory.getLogger(ExceptionFactory.class);

    public static ApplicationSpecificException create(final Throwable cause, final ExceptionType exceptionType, final Object... messageArguments) {
        LOG.error(MessageFormat.format(exceptionType.getMessage(), messageArguments), cause);
        return new ApplicationSpecificException (exceptionType, cause, messageArguments);
    }

    public static ApplicationSpecificException create(final ExceptionType exceptionType, final Object... messageArguments) {
        LOG.error(MessageFormat.format(exceptionType.getMessage(), messageArguments));
        return new ApplicationSpecificException(exceptionType, messageArguments);
    }
}
  1. Is it appropriate to use logging in any of the above mentioned layers?
  2. Log4j has log level such as FATAL, ERROR, WARN, INFO, DEBUG and TRACE . How to identify the situations to use these level when logging in Spring applications ?
1
Honestly, I don't see the purpose for a separate Exception-Layer. What are you trying to accomplish with that?JanTheGun
I have used a factory make it a cross cutting concern.Find more in this link stackoverflow.com/questions/45034371/…Harsha Jayamanna

1 Answers

1
votes

The answer to your first question: Logging is a so called "cross cutting concern" which means it is not related to the problem domain and so it occurrs everywhere in the code, in every layer.

Your second question is answered here: How to determine what log level to use?