Writing a java custom rule checking debug/trace level

Hi!

I’m trying to write a customized java rule to avoid excessive logging. Therefore the log levels must be checked. If the log level check has been forgotten, the rule should report an issue. The following code is the example code for the rule:

package org.sonar.samples.java.checks;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

public class CheckDebugTraceLevel {
    
    private static final Log LOG = LogFactory.getLog(CheckDebugTraceLevel.class);

    public void foo() 
    { 
        LOG.debug("some debug text.."); // Noncompliant
        LOG.trace("some text.."); // Noncompliant

        if(LOG.isDebugEnabled()) {
            LOG.debug("some debug text..");
        }
        if(LOG.isTraceEnabled()) {
            LOG.trace("some text..");
        }
    } 
}

My question is how to request the if-statement with its condition in my custom rule and if the request of the var type is proper this way. I tried something like this in my rule :

public class DebugAndTraceRule extends BaseTreeVisitor implements JavaFileScanner
{
	private JavaFileScannerContext context;
	private boolean logFlag = false;

	@Override
	public void visitIfStatement(IfStatementTree tree) 
	{
		if(logFlag && !(tree.condition().toString().equalsIgnoreCase("LOG.isDebugEnabled()")
					 || tree.condition().toString().equalsIgnoreCase("LOG.isTraceEnabled()")))
		{
			context.reportIssue(this, tree, "debug/trace levels of your logger must be enabled!");
		}
		super.visitIfStatement(tree);
	}
	
	@Override
	public void visitVariable(VariableTree tree) {
		if(tree.type().toString().equalsIgnoreCase("log")) {
			logFlag = true;
		}
		super.visitVariable(tree);
	}

	@Override
	public void scanFile(JavaFileScannerContext context) {
		this.context = context;
		scan(context.getTree());
	}
}

If somebody could please help, I would really appreciate it.