How to change class log level in Hybris without restarting server?

Scenario

Sometime to debug the production issue, we want to enable log level for certain classes which were not configured in local.properties and we hope we could do it without restarting Hybris server.


Solution

Your hope will come true. Yes, we can do it simply through the groovy script.
  • Go to HAC > Console > Scripting language
  • Past the below groovy script
  • change your full class in setLogger method setLogger("de.hybris.platform.jalo.flexiblesearch.FlexibleSearch", "DEBUG");
  • execute the script

import org.apache.logging.log4j.*;
import org.apache.logging.log4j.core.config.*;
import de.hybris.platform.util.logging.log4j2.HybrisLoggerContext;

//Example 
setLogger("de.hybris.platform.jalo.flexiblesearch.FlexibleSearch", "DEBUG");

public String setLogger(String logClass, String logLevel ) {
        final HybrisLoggerContext loggerCtx = (HybrisLoggerContext) LogManager.getContext(false);
        final Configuration loggerCfg = loggerCtx.getConfiguration();
        LoggerConfig loggerConfig = loggerCfg.getLoggers().get(logClass);
        if (loggerConfig == null) {
            // create
            String additivity = "true";
            String includeLocation = "true";
            Property[] properties = null;
            AppenderRef[] refs = [];
            filter = null;
            LoggerConfig createdLoggerConfig = LoggerConfig.createLogger(
                    additivity,
                    Level.getLevel(logLevel),
                    logClass,
                    includeLocation,
                    refs,
                    properties,
                    loggerCfg,
                    filter
            );

            loggerCfg.addLogger(logClass, createdLoggerConfig);
        } else {            

    loggerCfg.getLoggers().get(logClass).setLevel(Level.getLevel(logLevel));
        }
        loggerCtx.updateLoggers();
    }

You can see your class will be added to logging list (HAC > Platform > Loggin). Now enjoy debugging!! hope this helps you.



Groovy source: hybrisMart


Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 6+ years experience in Hybris, Java, J2EE. Extensive hands-on experience in SAP Hybris development, third-party integrations with Hybris, project architecture and design... Read more

Comments

Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

Hybris backoffice customization