Skip to content

Instantly share code, notes, and snippets.

@firaja
Last active July 24, 2018 15:43
Show Gist options
  • Save firaja/99545b77405853b3bb8134f4f06af095 to your computer and use it in GitHub Desktop.
Save firaja/99545b77405853b3bb8134f4f06af095 to your computer and use it in GitHub Desktop.
HAC Logging Configuration in 6.0 and above
package your.package;
import de.hybris.platform.util.logging.log4j2.HybrisLoggerContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.PostConstruct;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
/**
* @author David Bertoldi
*
* @see <a href="https://jira.hybris.com/browse/ECP-1231">ECP-1231</a>
* @see <a href="http://logging.apache.org/log4j/2.x/manual/architecture.html"/>Log4j Architecture</a>
*/
public class HACLoggerScanner
{
private static final Logger LOG = LogManager.getLogger();
private Filter filter;
private Level level = Level.INFO;
private String appenderRef = "CONSOLE";
private HybrisLoggerContext hybrisLoggerContext;
private Configuration loggerConfiguration;
public HACLoggerScanner()
{
hybrisLoggerContext = (HybrisLoggerContext) LogManager.getContext(false);
loggerConfiguration = hybrisLoggerContext.getConfiguration();
final LoggerConfig rootLogger = loggerConfiguration.getRootLogger();
if (rootLogger != null)
{
if (rootLogger.getLevel() != null)
{
level = rootLogger.getLevel();
}
filter = rootLogger.getFilter();
}
}
@PostConstruct
public void process()
{
LOG.info("Adding active Loggers to the Hybris Logger Context...");
processLog4J2Loggers(loggerConfiguration);
LOG.info("Refreshing the Hybirs Logger Context...");
hybrisLoggerContext.updateLoggers();
LOG.info("Done!");
}
private void processLog4J2Loggers(final Configuration loggerConfiguration)
{
final LoggerContext logContext = (LoggerContext) LogManager
.getContext(false);
Collection<org.apache.logging.log4j.core.Logger> loggers = logContext.getLoggers();
for (org.apache.logging.log4j.core.Logger logger : loggers)
{
if (logger.getLevel() != null)
{
setLogger(loggerConfiguration, logger.getName(), Level.getLevel(logger.getLevel().toString()));
}
else
{
setLogger(loggerConfiguration, logger.getName(), Level.getLevel(level.toString()));
}
}
}
private void setLogger(final Configuration loggerConfiguration, final String logClass, final Level logLevel)
{
final LoggerConfig loggerConfig = loggerConfiguration.getLoggers().get(logClass);
List<AppenderRef> appenderRefs;
if (loggerConfig != null)
{
appenderRefs = loggerConfig.getAppenderRefs();
}
else
{
appenderRefs = new ArrayList<>();
appenderRefs.add(AppenderRef.createAppenderRef(appenderRef, level, filter));
}
final LoggerConfig createdLoggerConfig = LoggerConfig.createLogger(
true,
logLevel,
logClass,
"true",
appenderRefs == null ? null : appenderRefs.toArray(new AppenderRef[appenderRefs.size()]),
null,
loggerConfiguration,
filter
);
loggerConfiguration.addLogger(logClass, createdLoggerConfig);
}
}
<bean class="your.package.HACLoggerScanner"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment