Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] InvalidLogMessageFormat FP with StringFormattedMessage and ParameterizedMessage #3133

Closed
testation21 opened this issue Feb 23, 2021 · 0 comments · Fixed by #3138
Closed
Assignees
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@testation21
Copy link
Contributor

testation21 commented Feb 23, 2021

Affects PMD Version: 6.31

Rule: InvalidLogMessageFormat

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd-6.31.0/pmd_rules_java_errorprone.html#invalidlogmessageformat

Description:
In Log4j documents(https://logging.apache.org/log4j/2.x/manual/messages.html),
ParameterizedMessage and StringFormattedMessage are described.
However, PMD shows alarms for it.
Also, even if there are no place holder with Throwable type in parameter, it shows an alarm.

Case 1. shows StringFormattedMessage,
Case 2. shows ParameterizedMessage and no place holder example
which are from brooklyn-server project in Apach Software Foundation.

Please, check them.
Thank you.

Code Sample demonstrating the issue:
Case 1.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class TestInvalidLogMessageFormat{
        Logger log = new LogggerFactory(TestInvalidLogMessageFormat.class);
        public void testPMD(){
                log.debug("param %s", "1");
        }
}

Detection message:
TestInvalidLogMessageFormat.java:7: InvalidLogMessageFormat: Too many arguments, expected 0 argument but have 1

Case 2.

protected void logProblem(String type, Object val) {
        if (lastWasProblem && currentProblemLoggedAsWarning) {
        ...        
        } else {
        ...
            if (!lastWasProblem) {
                if (expiryTime <= nowTime) {
                    ...                    
                    if (log.isDebugEnabled() && val instanceof Throwable) {
                    /*whistled*/ log.debug("Trace for "+type+" reading "+getBriefDescription()+": "+val, (Throwable)val);
                    }
                }
                ...
            } else {
                if (expiryTime <= nowTime) {
                    ...
                    if (log.isDebugEnabled() && val instanceof Throwable) {
                    /*whistled*/ log.debug("Trace for "+type+" reading "+getBriefDescription()+": "+val, (Throwable)val);
                    }
                } else {
                    if (log.isDebugEnabled()) {
                    /*whistled*/ log.debug("Recurring {} reading {} in {} (still in grace period): {}", new Object[] {type, this, getBriefDescription(), val});
                    }
                }
            }
        }
    }

brooklyn-server/core/src/main/java/org/apache/brooklyn/core/feed/AttributePollHandler.java:167: InvalidLogMessageFormat: Too many arguments, expected 0 argument but have 1
brooklyn-server/core/src/main/java/org/apache/brooklyn/core/feed/AttributePollHandler.java:183: InvalidLogMessageFormat: Too many arguments, expected 0 argument but have 1
brooklyn-server/core/src/main/java/org/apache/brooklyn/core/feed/AttributePollHandler.java:186: InvalidLogMessageFormat: Missing arguments, expected 4 arguments but have 1

Expected outcome:
No Alarms.

Running PMD through: [CLI]

@testation21 testation21 added a:false-negative PMD doesn't flag a problematic piece of code a:false-positive PMD flags a piece of code that is not problematic labels Feb 23, 2021
@adangel adangel removed the a:false-negative PMD doesn't flag a problematic piece of code label Feb 25, 2021
@adangel adangel changed the title [java] InvalidLogMessageFormat FP in 6.31 [java] InvalidLogMessageFormat FP with StringFormattedMessage and ParameterizedMessage Feb 25, 2021
@adangel adangel self-assigned this Feb 25, 2021
@adangel adangel added this to the 6.32.0 milestone Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
2 participants