Add host information to logging

In a situation where you run multiple subsites, it can be very useful to know at which subsite (host) an error has occurred. You can modify your log pattern for this, to always prefix the log entry with the hostname of the subsite. Thus, for example, in your log4j2.xml use:

<Configuration packages="com.onehippo.logging">
<!-- site.log -->
<RollingFile name="site" fileName="${sys:catalina.base}/logs/site.log"
  <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"/>
    <SizeBasedTriggeringPolicy size="10MB"/>
  <DefaultRolloverStrategy max="4" fileIndex="min"/>

This is quite easy to do by using following pattern converter plugin:

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)
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;

    public void format(final Object obj, final StringBuilder toAppendTo) {

    private String getHostInfo() {
        final HstRequestContext context = RequestContextProvider.get();
        if (context != null) {
            final HstContainerURL baseURL = context.getBaseURL();
            if (baseURL != null) {
                return baseURL.getHostName();
        return "";

Log4j2 starts searching for available plugins during it's initialization phase, and thus HostPatternConverter should be available at that time. It can be done by placing plugin file into same location as log4j libraries.

Compile time dependencies are required for:


Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?