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 log4j.xml use:
<!-- hippo-site.log --> <appender name="site" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="${catalina.base}/logs/hippo-site.log"/> <param name="Append" value="true"/> <layout class="com.onehippo.logging.HostPatternLayout"> <param name="ConversionPattern" value="%h %d{dd.MM.yyyy HH:mm:ss} %-5p [%C.%M():%L] %m%n"/> </layout> <filter class="org.onehippo.cms7.logging.log4j.MdcOrJndiPropertyFilter"> <param name="name" value="logging/contextName" /> <param name="resourceRef" value="true" /> <param name="value" value="site" /> <param name="onMatchOption" value="ACCEPT" /> </filter> </appender>
This is quite easy to do by using following layout pattern:
package com.onehippo.logging; import org.apache.log4j.PatternLayout; import org.apache.log4j.helpers.PatternConverter; import org.apache.log4j.helpers.PatternParser; import org.apache.log4j.spi.LoggingEvent; import org.hippoecm.hst.container.RequestContextProvider; import org.hippoecm.hst.core.container.HstContainerURL; import org.hippoecm.hst.core.request.HstRequestContext; /** * Adding a hostname pattern to log4j pattern: <strong>{@code h}</strong> * <p/> * Usage example: * <pre> * <strong> * <layout class="com.onehippo.logging.HostPatternLayout"> * <param name="ConversionPattern" value="%h - * %d{dd.MM.yyyy HH:mm:ss} %-5p [%C.%M():%L] %m%n"/> * </layout> * </strong> * </pre> * * @version "$Id$" */ public class HostPatternLayout extends PatternLayout { @Override protected PatternParser createPatternParser(final String pattern) { return new PatternParser(pattern) { @Override protected void finalizeConverter(char c) { PatternConverter patternConverter = null; if (c == 'h') { patternConverter = new PatternConverter() { @Override protected String convert(LoggingEvent event) { HstRequestContext context = RequestContextProvider.get(); if (context != null) { final HstContainerURL baseURL = context.getBaseURL(); if (baseURL != null) { return baseURL.getHostName(); } } return ""; } }; } if (patternConverter == null) { super.finalizeConverter(c); } else { addConverter(patternConverter); } } }; } }
Compile time dependencies are required for:
<properties> <lib.slf4j.version>1.7.6</lib.slf4j.version> <lib.log4j.version>1.2.17</lib.log4j.version> <hippo.hst.version>2.xx.yy</hippo.hst.version> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <scope>provided</scope> <version>${lib.slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${lib.log4j.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>