何时何地在spring-boot应用程序中应用日志记录

时间:2017-08-03 10:05:57

标签: java spring logging log4j

我设置了一个spring-boot应用程序,包括使用log4j进行日志记录。在应用程序中,有几个层,例如controller, service, models, repositories, exception等。目前,我已在exception层中包含 ERROR 级别日志。

@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. 在上述任何一个层中使用日志记录是否合适?
  2. Log4j具有日志级别,例如致命错误警告信息 DEBUG < / strong>和跟踪。如何在登录Spring应用程序时识别使用这些级别的情况?

1 个答案:

答案 0 :(得分:1)

第一个问题的答案: 记录是一种所谓的“交叉问题”,这意味着它与问题域无关,因此它在代码中的每个层都出现。

您的第二个问题在这里得到解答: How to determine what log level to use?