Reputation: 1281
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());
}
}
Upvotes: 0
Views: 796
Reputation: 105
First, note the Constants.SPRING_PROFILE_DEVELOPMENT
in your code.
Go to your /config/LoggingAspectConfiguration.java
file and check the @Profile
annotation. In your case, I imagine that you have: JHipsterConstants.SPRING_PROFILE_DEVELOPMENT
passed in there.
If so, there are two ways to solve this:
SPRING_PROFILE_PRODUCTION
constant instead wherever applicable, and SPRING_PROFILE_DEVELOPMENT
when you want that logging weaved in with dev. While this is quick and easy, it also will probably get you a level of logging you might not want in prod, depending on other settings (e.g. what you have configured in your jhipster Logs page).aop.logging
directory, create another Java class that mirrors LoggingAspect
. Let's say it's called ResourceLoggingAspect
. Use what is in LoggingAspect
out-of-the-box with jhipster as a starting point for that file, but you don't have to include all the advice therein. Instead, replace their advice with your own. For instance:@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());
}
}
Now just ensure logging is at the INFO level in Prod, and you will see the output. If you use my example above, you'll see user logins being logged in your Controller layer. You can replace my example above with your own AfterThrowing
advice.
Upvotes: 1