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());
}
}
答案 0 :(得分:0)
首先,请注意代码中的Constants.SPRING_PROFILE_DEVELOPMENT
。
转到您的/config/LoggingAspectConfiguration.java
文件并检查@Profile
批注。就您而言,我想您有:JHipsterConstants.SPRING_PROFILE_DEVELOPMENT
传入那里。
如果是这样,有两种解决方法:
SPRING_PROFILE_PRODUCTION
常量,并在需要使用dev编织日志时使用SPRING_PROFILE_DEVELOPMENT
。虽然这是快速简便的操作,但也可能会为您提供您可能不需要的产品日志记录级别,具体取决于其他设置(例如,您在jhipster日志页面中配置的内容)。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
建议代替上面的示例。