Log4j for filtering methods

I want to disable logging on a class method. see below:

package com.mypackage;

public class A {
    public static void aaa() { 
        logger.info("hello"); 
    }

    public static void bbb() { 
        logger.info("hello"); 
    }
}

      

...

# This works and nothing gets logged by A
log4j.category.com.mypackage.A=off

      

...

#this does not work. (I am trying to switch off only the static method)
log4j.category.com.mypackage.A.aaa=off

      

Does anyone know if it is possible to disable (change the log level) based on the method?

thank

Note. I don't want to change the code (which, since the code is already live, I only want to change the existing log4j, which is only a config file)

+3


source to share


3 answers


I am not aware of the possibility of doing this.

However, you can customize the logging to include the class and method name (see PatternLayout ) and post the log file using other tools like: ' grep

' to include / exclude what you want to see.



Due to performance issues, it is not recommended to write down the names of classes and methods, but it may not be the case for you.

+1


source


What you can do is have two different loggers, one for each method.

private static final Logger logAAA = Logger.getLogger("some.logger.name.for.AAA");
private static final Logger logBBB = Logger.getLogger("some.other.name.for.BBB");

      



And then define different log levels for them in your configuration.

+3


source


This is actually possible by implementing a specific Filter

one that will check the name of the registrar and then the calling method. Your filter gets LoggingEvent

, which lets you get LocationInfo

with the name of the class and method.

Note that this will use reflection in the stack trace, which can be quite inefficient, do it with care. In particular, check the log name first before fetching LocationInfo

so that you only do reflection when needed.

Filters can be configured only through the XML Log4j configuration file.

Fast implementation:

Filter class:

public class MethodBasedFilter extends Filter {

    @Override
    public int decide(LoggingEvent event) {
        if (event.getLoggerName().equals(LoggingWithMethodFiltering.class.getCanonicalName())) {
            if (event.getLocationInformation().getMethodName().equals("filteredMethod")) {
                return Filter.DENY;
            }
        }
        return Filter.NEUTRAL;
    }
}

      

Filtered class:

public class LoggingWithMethodFiltering {
    private static final Logger LOG = Logger.getLogger(LoggingWithMethodFiltering.class);

    public static void unfilteredMethod() {
        LOG.info("I am not filtered");
    }

    public static void filteredMethod() {
        LOG.info("I am filtered and will never appear");
    }

    public static void main(String[] args) {
        unfilteredMethod();
        filteredMethod();
    }
}

      

log4j.xml

:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>

    <appender name="Console" class="org.apache.log4j.ConsoleAppender">
        <param name="threshold" value="trace" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p|%-20.20t|%d{HH:mm:ss,SSS}|%20.30c{1} %25.25F:%-4L - %20.20M - %m%n" />
        </layout>
        <filter class="MethodBasedFilter" />
    </appender>

    <root>
        <level value="trace" />
        <appender-ref ref="Console" />
    </root>

</log4j:configuration>

      

+3


source







All Articles