Jhipster : error log doesn't show with prod profile

时间:2019-04-08 14:00:48

标签: logging jhipster

I am facing something I can't explain. I have set log level to INFO in both dev and prod profiles in the pom.xml.

When I run the web app with nohup /home/dev/workspace/myapp/target/myapp.war --spring.profiles.active=dev

The following instruction double division = 1/0; shows an error in the log console :

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

When I run with prod profile :

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

Nothing displays in the log console. I don't understand why, because I have set the same log level in both profiles.

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());
        }
    }

1 个答案:

答案 0 :(得分:0)

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

转到您的/config/LoggingAspectConfiguration.java文件并检查@Profile批注。就您而言,我想您有:JHipsterConstants.SPRING_PROFILE_DEVELOPMENT传入那里。

如果是这样,有两种解决方法:

  1. 在适当的地方改用SPRING_PROFILE_PRODUCTION常量,并在需要使用dev编织日志时使用SPRING_PROFILE_DEVELOPMENT。虽然这是快速简便的操作,但也可能会为您提供您可能不需要的产品日志记录级别,具体取决于其他设置(例如,您在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级别,您将看到输出。如果使用上面的示例,则将在Controller层中看到用户登录名。您可以用自己的AfterThrowing建议代替上面的示例。