green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[WEB][42108] Best practice use logger.error #81

Open JulienBertrand opened 1 year ago

JulienBertrand commented 1 year ago

\newpage

Best practice use logger.error

Platform

OS OS version Langage
{Windows} {} {JAVA}

Main caracteristics

ID Title Category Sub-category
CRJVM005-42108 {Wrong use of logger.error for logging } {Logging} {Aggregator Logger}

Severity / Remediation Cost

Severity Remediation Cost
Minor Minor

Rule short description

Rule complete description

Text

{When running an application, loggers in error severity shouldn't be run in the main body of a method. In production, an application with many users can result in a very large amount of logs and costly in performance. Incorrect use of loggers and their severity can result in too many network calls and memory overflow of splunk servers. So, It's recommented to use the error severity of your logger in a catch or failure zone of the program.}

JAVA Example

// Uncompliant code:
public void save(){
    double currentValue = generateValue();
    if (currentValue != 0) {
        logger.error("Before Save");
        saveCurrentValue(currentValue);
    }
}

// Compliant code:
public void save(){
    double currentValue = generateValue();
    if (currentValue != 0) {
        try{
            logger.debug("Before Save");
            saveCurrentValue(currentValue);
        } catch(Exception e){
            logger.error(e.getStackTrace());
        }
    }
}

Implementation principle

Source for understanding the difference in the use of logger severity: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html

jhertout commented 2 months ago

Even if it seems to be something for ecoCode in this rule, I can see several problems:

For me, this rule needs more work before being implemented in ecoCode.