loading table of contents...

4.3.7.1. The Logging Component

This component provides a common logging infrastructure based on the logback framework. The component adds some new features:

  • Automatic registration of a configuration MBean as com.coremedia:Type=Logging,application=<applicationname>

  • A fallback logging configuration that is loaded when the application doesn't provide a more specific one. You will find this configuration in the file /META-INF/resources/WEB-INF/logback-default.xml in the logging-component.jar file.

  • Preconfigured logging appenders that can be simply imported from log configuration files. Two profiles ("development" and "production") have been defined that hold slightly different appender configurations. The "development" profile logs more information for development issues while the "production" profile should be used in a live system.

Adding the Logging Component

You can simply add the CoreMedia logging component to your CoreMedia web application as described below. For your custom services and code, you should use slf4j as a logging interface. When using the logging component, slf4j will be automatically bound to the logback logger implementation.

Adding dependency

Adding the component simply consists of adding the following dependency to your web application's project:

<dependency>
 <groupId>com.coremedia.cms</groupId>
 <artifactId>logging-component</artifactId>
 <scope>runtime</scope>
</dependency>

Example 4.35. Maven Dependency for Logging


Adding log configuration

If you want to use your own log configuration, add a file /WEB-INF/logback.xml to your application that looks like the following:

<configuration>
  <!--
    includes a common configuration that dispatches to
    "coremedia-development-profile.xml"
    or
    "coremedia-production-profile.xml" depending
    on property "coremedia.logging.profile".

    You can define a custom log pattern ("log.pattern") or a custom log file name ("log.file") like this:
    <property name="log.pattern" value="%d %-7([%level]) %logger - [%X{tenant}] %message \\(%thread\\)%n" />

  -->
  <include resource="logging-common.xml"/>

  <!-- adds project specific logger and references a common appender -->
  <logger name="com.coremedia" additivity="false" level="info">
    <appender-ref ref="file"/>
  </logger>

  <root level="warn">
    <appender-ref ref="file"/>
  </root>
</configuration>

Example 4.36. Logback Configuration


Changing log directory

By default, logs are written in the ./logs directory. If you want to change this directory you have to either pass a system property coremedia.logging.directory or a JNDI property java:comp/env/coremedia/logging/directory to the application. For example, when you use Tomcat you can add the following code to context.xml:

<Context ...>
 ...
  <Environment name="coremedia/logging/directory"
   value="./my-log-dir" type="java.lang.String" override="true"/>
 ...
</Context>

Example 4.37. Change Log Directory in Tomcat


See the Tomcat documentation for more details.

Switching the log profile

If you want to switch the logging profile from the default "production" to "development", you have to either set the system property coremedia.logging.profile or the JNDI property java:comp/env/coremedia/logging/profile to the "development" value.

Changing log configuration at runtime

If you want to change the logback configuration during runtime, there are two options: You can either use JMX or logback's automatic reloading mechanism. To let logback reload the configuration, you have to add the scan attribute to the configuration element.

<configuration scan="true" scanPeriod="30 seconds">
  ...
</configuration>

Example 4.38. Automatically reload configuration file every 30 seconds


[Note]Note

Consider that the scan feature only works for configuration loaded from the file system. It does not work for classpath or web application resources. To this end you have to override the property coremedia.logging.configuration and provide a file URL pointing to the logback configuration (such as file:/path/to/logback.xml). The property can be set via JNDI or as system property.