0

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 ?
Harsha Jayamanna
  • 2,148
  • 6
  • 25
  • 43
  • Honestly, I don't see the purpose for a separate Exception-Layer. What are you trying to accomplish with that? – JanTheGun Aug 03 '17 at 10:26
  • I have used a factory make it a cross cutting concern.Find more in this link https://stackoverflow.com/questions/45034371/where-to-handle-exceptions-in-spring-applications – Harsha Jayamanna Aug 03 '17 at 10:27

1 Answers1

1

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?

JanTheGun
  • 2,165
  • 17
  • 25