Add Host Information to Logging

When running multiple subsites, it is important to identify which subsite (host) generates each log entry. You can configure your logging pattern to include the hostname, making it easier to trace errors to specific subsites.

Update the Log Pattern

To add the host information to each log entry, update your log4j2.xml configuration. For example:

<Configuration packages="com.onehippo.logging"> .... <!-- site.log --> <RollingFile name="site" fileName="${sys:catalina.base}/logs/site.log" filePattern="${sys:catalina.base}/logs/site.log.%d{yyyy-MM-dd}"> <LookupFilter key="jndi:logging/contextName" value="site" onMatch="ACCEPT"/> <PatternLayout pattern="%host %d{dd.MM.yyyy HH:mm:ss} [%t] %-5p [%C.%M():%L] %m%n"/> <Policies> <SizeBasedTriggeringPolicy size="10MB"/> </Policies> <DefaultRolloverStrategy max="4" fileIndex="min"/> </RollingFile>

The %host pattern in the PatternLayout inserts the hostname for each log entry.

Implement the Host Pattern Converter Plugin

To support the %host pattern, implement a custom Log4j2 pattern converter plugin. Use the following example:

package com.onehippo.logging; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.pattern.AbstractPatternConverter; import org.apache.logging.log4j.core.pattern.ConverterKeys; import org.apache.logging.log4j.core.pattern.PatternConverter; import org.hippoecm.hst.container.RequestContextProvider; import org.hippoecm.hst.core.container.HstContainerURL; import org.hippoecm.hst.core.request.HstRequestContext; @Plugin(name="HostConverter", category = PatternConverter.CATEGORY) @ConverterKeys({"host"}) public class HostPatternConverter extends AbstractPatternConverter { private static final HostPatternConverter INSTANCE = new HostPatternConverter(); private HostPatternConverter() { super("host", "host"); } public static HostPatternConverter newInstance(final String[] options) { return INSTANCE; } @Override public void format(final Object obj, final StringBuilder toAppendTo) { toAppendTo.append(getHostInfo()); } private String getHostInfo() { final HstRequestContext context = RequestContextProvider.get(); if (context != null) { final HstContainerURL baseURL = context.getBaseURL(); if (baseURL != null) { return baseURL.getHostName(); } } return ""; } }

This plugin retrieves the current host name from the request context and appends it to each log entry.

Register the Plugin

Log4j2 scans for available plugins during initialization. Ensure that the HostPatternConverter class is available on the classpath at startup. Place the compiled plugin in the same directory as your Log4j libraries.

Add Compile-Time Dependencies

Add the following dependencies to your project to compile the plugin:

<properties> <log4j2.version>2.8.2</log4j2.version> <hippo.hst.version>5.xx.yy</hippo.hst.version> </properties> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.onehippo.cms7.hst</groupId> <artifactId>hst-commons</artifactId> <version>${hippo.hst.version}</version> <scope>provided</scope> </dependency> </dependencies>

Verification

After deploying the plugin and updating your log configuration, verify that each log entry includes the correct hostname for the subsite handling the request. Check your log files to confirm the presence of the host information at the beginning of each entry.

Share Feedback
Page: /build/development-tools/add-host-information-to-logging
Section: Build
Category *