jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.54k stars 4.02k forks source link

LoggingAspect code generated may not print cause when error #21304

Closed Farley-Chen closed 1 year ago

Farley-Chen commented 1 year ago
Overview of the issue

it's a easy bug.

The LoggingAspect generated code like this.

@AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
        if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
            logger(joinPoint)
                .error(
                    "Exception in {}() with cause = '{}' and exception = '{}'",
                    joinPoint.getSignature().getName(),
                    e.getCause() != null ? e.getCause() : "NULL",
                    e.getMessage(),
                    e
                );
        } else {
            logger(joinPoint)
                .error(
                    "Exception in {}() with cause = {}",
                    joinPoint.getSignature().getName(),
                    e.getCause() != null ? e.getCause() : "NULL"
                );
        }
    }

The second logger.error() may not work rightly with logback, because the cause is a exception.

logger(joinPoint)
    .error(
        "Exception in {}() with cause = {}",
        joinPoint.getSignature().getName(),
        e.getCause() != null ? e.getCause() : "NULL"
    );

maybe like this

ERROR com.demo.node.aop.logging.LoggingAspect - Exception in method2() with cause = {}
java.lang.Exception: cause
    at com.demo.node.aop.logging.LoggingAspect.main(LoggingAspect.java:32)
Motivation for or Use Case
Reproduce the error
Related issues
Suggest a Fix

Maybe use String.valueof() to wrap the cause.

Generated code maybe like this.

if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
            logger(joinPoint)
                .error(
                    "Exception in {}() with cause = '{}' and exception = '{}'",
                    joinPoint.getSignature().getName(),
                    e.getCause() != null ? e.getCause() : "NULL",
                    e.getMessage(),
                    e
                );
        } else {
            logger(joinPoint)
                .error(
                    "Exception in {}() with cause = {}",
                    joinPoint.getSignature().getName(),
                    String.valueOf(e.getCause() != null ? e.getCause() : "NULL")
                );
        }

I still don't know why IDE say that "Unnecessary 'String.valueOf()' call". It's worked in my project with jdk17.

JHipster Version(s)

version 7.9.3

JHipster configuration
Entity configuration(s) entityName.json files generated in the .jhipster directory
Browsers and Operating System
github-actions[bot] commented 1 year ago

This issue is stale because it has been open for too long without any activity. Due to the moving nature of jhipster generated application, bugs can become invalid. If this issue still applies please comment otherwise it will be closed in 7 days

mshima commented 1 year ago

@Farley-Chen can you contribute with a PR?

Farley-Chen commented 1 year ago

@mshima OK, I have contribute the PR

I think I'm not familiar with Javascript so I didn't contribute before.

So just now I have found the right place and correct it with the PR.