Context-Based Logging - Enterprise Java Content management system - Hippo CMS

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 the site webapp always end up in hippo-site.log, while those that originate in the cms webapp end up in hippo-cms.log.

The default log4j configuration files are:

  • conf/log4j2-dev.xml
    Used when running 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 when creating 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 the log4j2-*.xml files the cms and site appenders have the LookupFilter below:

  <Appenders>

    <!-- hippo-cms.log -->
    <RollingFile name="cms" fileName="${sys:catalina.base}/logs/hippo-cms.log"
                 filePattern="${sys:catalina.base}/logs/hippo-cms.log.%i">
      <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>

    <!-- hippo-site.log -->
    <RollingFile name="site" fileName="${sys:catalina.base}/logs/hippo-site.log"
                 filePattern="${sys:catalina.base}/logs/hippo-site.log.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 the web.xml of the cms 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 the web.xml of the site 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>

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 the site webapp always end up in hippo-site.log, while those that originate in the cms webapp end up in hippo-cms.log.

The default log4j configuration files are:

  • conf/log4j2-dev.xml
    Used when running 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 when creating 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 the log4j2-*.xml files the cms and site appenders have the LookupFilter below:

  <Appenders>

    <!-- hippo-cms.log -->
    <RollingFile name="cms" fileName="${sys:catalina.base}/logs/hippo-cms.log"
                 filePattern="${sys:catalina.base}/logs/hippo-cms.log.%i">
      <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>

    <!-- hippo-site.log -->
    <RollingFile name="site" fileName="${sys:catalina.base}/logs/hippo-site.log"
                 filePattern="${sys:catalina.base}/logs/hippo-site.log.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 the web.xml of the cms 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 the web.xml of the site 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>