Context-Based Logging
Introduction
Projects created using Hippo's Maven archetype include default Log4j 2 configuration files that use context-based logging. This means that log messages that originate in thesite webapp always end up insite.log, while those that originate in thecms webapp end up incms.log.
The default log4j configuration files are:
- conf/log4j2-dev.xml
Used whenrunning in a local Cargo-based development environment. Configures daily log file rollover and includes sensible log levels for development. Also includes a console appender which logs to the command line console in which the application is running. - conf/log4j2-dist.xml
Optimized for production environments and included whencreating a project distribution. Configures log file rollover based on size and includes only log levels that are relevant in production environments.
If your project was not created using the archetype, or was created using an older version of the archetype, you can update your Log4j configuration to reflect current best practice by following the instructions on this page.
Configure Log4j 2
Make sure that in thelog4j2-*.xml files thecms andsite appenders have theLookupFilter below:
<Appenders> <!-- cms.log --> <RollingFile name="cms" fileName="${sys:catalina.base}/logs/cms.log" filePattern="${sys:catalina.base}/logs/cms.log.%d{yyyy-MM-dd}"> <LookupFilter key="jndi:logging/contextName" value="cms" onMatch="ACCEPT"/> <PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss} [%t] %-5p [%C.%M():%L] %m%n"/> <Policies> <SizeBasedTriggeringPolicy size="10MB"/> </Policies> <DefaultRolloverStrategy max="1" fileIndex="min"/> </RollingFile> <!-- 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="%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> </Appenders>Add to the end of theweb.xml of thecms webapp the following part:
<env-entry> <env-entry-name>logging/contextName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>cms</env-entry-value></env-entry>
And add to the end of theweb.xml of thesite webapp the following part:
<env-entry> <env-entry-name>logging/contextName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>site</env-entry-value></env-entry>