How to change class log level in Hybris without restarting server?
Scenario
Sometimes 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 the Hybris server.
Solution
If your hybris version > v6 you can easily do this by navigating hac/platform/log4j . Here you can add your full class package and the log level you want.
In the older hybris version, you can do the same using 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
Comments
Post a Comment