【问题标题】:Jhipster : error log doesn't show with prod profileJhipster:错误日志不显示在产品配置文件中
【发布时间】:2019-04-08 14:00:47
【问题描述】:

我正面临着我无法解释的事情。 我在 pom.xml 的 dev 和 prod 配置文件中都将日志级别设置为 INFO。

当我使用nohup /home/dev/workspace/myapp/target/myapp.war --spring.profiles.active=dev 运行网络应用程序时

下面的指令双除= 1/0;在日志控制台中显示错误:

08/04/2019 - 15:37 [ERROR] com.myapp.rh.aop.logging.LoggingAspect - Exception in com.myapp.rh.service.MailService.sendRetourCandidatureEmail() with cause = null and exception {}
java.lang.ArithmeticException: / by zero

当我使用 prod 配置文件运行时:

nohup /home/dev/workspace/myapp/target/myapp.war --spring.profiles.active=prod

日志控制台中不显示任何内容。我不明白为什么,因为我在两个配置文件中设置了相同的日志级别。

LogginAspect.java:

@AfterThrowing(pointcut = "loggingPointcut()", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
        if (env.acceptsProfiles(Constants.SPRING_PROFILE_DEVELOPMENT)) {
            log.error("Exception in {}.{}() with cause = {} and exception {}", joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName(), e.getCause(), e);
        } else {
            log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(),
                joinPoint.getSignature().getName(), e.getCause());
        }
    }

【问题讨论】:

    标签: logging jhipster


    【解决方案1】:

    首先,请注意代码中的Constants.SPRING_PROFILE_DEVELOPMENT

    转到您的/config/LoggingAspectConfiguration.java 文件并检查@Profile 注释。在你的情况下,我想你有:JHipsterConstants.SPRING_PROFILE_DEVELOPMENT 在那里传递。

    如果是这样,有两种方法可以解决这个问题:

    1. 在适用的情况下使用SPRING_PROFILE_PRODUCTION 常量,当您希望将日志记录与dev 结合时使用SPRING_PROFILE_DEVELOPMENT。虽然这既快速又简单,但它也可能会为您提供在 prod 中可能不需要的日志记录级别,具体取决于其他设置(例如,您在 jhipster 日志页面中配置的内容)。
    2. 在您的aop.logging 目录中,创建另一个反映LoggingAspect 的Java 类。假设它被称为ResourceLoggingAspect。使用开箱即用的LoggingAspect 中的内容和jhipster 作为该文件的起点,但您不必在其中包含所有advice。相反,用你自己的建议代替他们的建议。例如:
    @Aspect
    public class ResourceLoggingAspect {
    
        private final Logger log = LoggerFactory.getLogger(this.getClass());
    
        private final Environment env;
    
        public ResourceLoggingAspect(Environment env) {
            this.env = env;
        }
    
        /**
         * Pointcut that matches all repositories, services and Web REST endpoints.
         */
        @Pointcut("within(@org.springframework.stereotype.Repository *)" +
            " || within(@org.springframework.stereotype.Service *)" +
            " || within(@org.springframework.web.bind.annotation.RestController *)")
        public void springBeanPointcut() {
            // Method is empty as this is just a Pointcut, the implementations are in the advices.
        }
    
        /**
         * Pointcut that matches all Spring beans in the application's main packages.
         */
        @Pointcut("within(com.yourapp.repository..*)"+
            " || within(com.yourapp.service..*)"+
            " || within(com.yourapp.web.rest..*)")
        public void applicationPackagePointcut() {
            // Method is empty as this is just a Pointcut, the implementations are in the advices.
        }
    
        /**
         * Add another advice here. Will use @Before as an example
         */
        @Before("execution(* com.yourapp.web.rest..*(..))")
        public void logBefore(JoinPoint joinPoint) {
            log.info("Username: " + SecurityUtils.getCurrentUserLogin() + " has made a call to " + joinPoint.getSignature().getName());
        }
    }
    

    现在只需确保日志记录在 Prod 中的 INFO 级别,您就会看到输出。如果你使用我上面的例子,你会看到用户登录被登录到你的控制器层。您可以用您自己的AfterThrowing 建议替换我上面的示例。

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      相关资源
      最近更新 更多