Dario
Dario

Reputation: 3

Redirect logger messages from abstract child class that inherits Logger from parent

I'm working on a Java project (not mine) that uses spring-beans among many other dependencies. My goal is to redirect all the logger messages from several spring-beans classes to a file so that they're not visible in the console log. I went with the programmatic approach because that's what suits me more in this situation and after googling some Log4j solutions, found one that (almost) works for me:

public static void removeConsoleLogsByClassName(String fullClassName) {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    PatternLayout layout = PatternLayout.newBuilder()
            .withPattern("%d{yyyy-MM-dd HH:mm:ss} %c{2} | [%t] | %-5level: - %m%n")
            .withConfiguration(config).build();
    Appender appender = FileAppender.createAppender(
             "some_location_goes_here/logfile.log",
            "false",
            "false", "fileAppender", "true", "false", "false", "8192", layout, null, "false", null, config);
    appender.start();
    config.addAppender(appender);
    AppenderRef ref = AppenderRef.createAppenderRef("fileAppender", null, null);
    AppenderRef[] refs = new AppenderRef[] { ref };
    org.apache.logging.log4j.core.config.LoggerConfig loggerConfig = LoggerConfig.createLogger(false,
            org.apache.logging.log4j.Level.TRACE,
            fullClassName,
            "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    if ("false".equals(TestInfo.getEnableLog())) {
        loggerConfig.setLevel(org.apache.logging.log4j.Level.OFF);
    }
    config.addLogger(fullClassName, loggerConfig);
    ctx.updateLoggers();
}

With this method, i can provide the Class name, and programmaticaly redirect the logger messages to a file, which is exactly what i need. This works except for the (abstract) classes inside spring-beans that inherit LOGGER object from some parent class.

In my case, i'm trying to redirect log messages from AbstractBeanFactory to a file, but they still show up in console. I presume it's because this class inherits the LOGGER object from DefaultSingletonBeanRegistry, but i'm not sure.

Classes hierarchy

If anyone has any advice/solution for this, i would appreciate it greatly. Thanks in advance.

Upvotes: 0

Views: 900

Answers (1)

matanper
matanper

Reputation: 931

If you look at the source code of the top class in the hierarchy (SimpleAliasRegistry) you'll see the initialization of the logger:

/** Logger available to subclasses. */
protected final Log logger = LogFactory.getLog(getClass());

The getClass() returns the type of the concrete class in runtime. so when you see the abstract class in the log, this is the location the log was written, but the real class which gives the logger his name is some subclass.
I'd suggest you try to change the pattern to all the logs, and then it will print the name of the real logger name (which is the subclass)

Upvotes: 1

Related Questions