Search code examples
javalog4jlog4j2jmxmbeanexporter

LoggerDynamicMBean log4j 1.x to log42


So I was trying to migrate our legacy application java log4j 1.x to log4j2

I saw a great article with regards to migrating this log4j using the bridge API. However, there's one thing that I am stuck with.

Here is the culprit

   ObjectName addLoggerMBean(MBeanServer server, Logger logger)
{
    String name = logger.getName();
    ObjectName objectName = null;
    
    try
    {
        LoggerDynamicMBean loggerMBean = new LoggerDynamicMBean(logger);
        objectName = new ObjectName("log4j.category", "logger", name);

        if (!server.isRegistered(objectName))
        {
            server.registerMBean(loggerMBean, objectName);
           
        }
    }
    catch (Exception e)
    {
        // log.error("Could not add loggerMBean for ["+name+"].", e);
    }
    
    return objectName;
}

So the snippet above was creating the instance of LoggerDynamicMBean under the library of org.apache.log4j.jmx.LoggerDynamicMBean and unfortunately, it seems that this is not part of the existing log4j bridge API.

Does anyone know how to achieve this? Sorry for the nub question, just new on java an this application doesn't even run locally. So think of that :D


Solution

  • The code in your question just exports some logger data though JMX for a monitoring tool. Performing a migration is the best time to reevaluate the utility of code like this.

    If you don't use JMX monitoring tools just drop the code.

    If you do use JMX monitoring tools, the code is also useless, since Log4j 2.x exports data through JMX by default (cf. LoggerConfigAdminMBean). You'll need however to adapt the monitoring tool for a different set of data.