Java – log4j filter out method

I want to turn off logging for a class method

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 we can turn off (change the log level) based on the method?

thank you

Note: I don't want to change the code (that's because the code already exists. I just want to change the existing log4j, which is just a configuration file)

Solution

I didn't know it was possible

However, you can configure logging to include class and method names (see patternlayout) and post process the log file with other tools such as "grep" to include / exclude what you want to view

Recording class and method names is not recommended due to performance problems, but it may not be your concern

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>