【问题标题】:When and Where to apply logging in spring-boot application何时何地在 spring-boot 应用程序中应用日志记录
【发布时间】:2017-08-03 10:05:57
【问题描述】:

我设置了一个 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. Lo​​g4j 有 FATALERRORWARNINFODEBUG 和 TRACE 。登录 Spring 应用程序时如何识别使用这些级别的情况?

【问题讨论】:

  • 老实说,我看不出单独的异常层的目的。你想用它来完成什么?
  • 我使用了一家工厂,使其成为横切关注点。在此链接中查找更多信息stackoverflow.com/questions/45034371/…

标签: java spring logging log4j


【解决方案1】:

第一个问题的答案: 日志记录是所谓的“横切关注点”,这意味着它与问题域无关,因此它出现在代码的每一处、每一层。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 2017-01-02
    • 2016-05-30
    • 2019-05-11
    • 2020-12-01
    相关资源
    最近更新 更多