Using a custom LoggerManager
Say, we want to use Log4j logging for the log messages in our Website monitor component - how could we replace the default logging implementation?
Before we demonstrate how logger can be swapped, some notes on how logging is setup up for a Plexus component
- Logging for a component is handled using a org.codehaus.plexus.logging.LoggerManager component.
- LoggerManager makes a org.codehaus.plexus.logging.Logger instance available to the Plexus container. The LoggerManager can create (or obtain) as well as configure the Logger instance before making it available to the Plexus container.
- Each component in Plexus runtime get its own Logger instance.
- LoggerManager is initialized on Plexus container startup. It is for this reason, the LoggerManager should be overridden via Plexus container's configuration.
From what we have coded up so far, we don't have Plexus container configuration available to us, nor is it desirable to run our component in a Plexus container without being fully tested. So here is what we do:
- Override the configuration used by PlexusTestCase to set up a Plexus Container instance.
- Setup the Log4jLoggerManager in the overridden configuration.
For step (1) above we update the WebMonitorTest and override the getCustomConfiguration() method from PlexusTestCase as follows:
protected InputStream getCustomConfiguration() throws Exception {
InputStream is = this.getClass ().getClassLoader ().getResourceAsStream ("org/codehaus/plexus/PlexusTestContainerConfig.xml");
return is;
}
For step (2), create an XML configuration file under <project-root>/src/test/resources/org/codehaus/plexus/PlexusTestContainerConfig.xml, with following contents:
<!-- Override the configuration that is used by the PlexusTestCase to create a Container instance -->
<plexus>
<components>
<component>
<role>org.codehaus.plexus.logging.LoggerManager</role>
<implementation>
org.codehaus.plexus.logging.log4j.Log4JLoggerManager
</implementation>
<configuration>
<appenders>
<appender>
<id>rolling</id>
<threshold>DEBUG</threshold>
<type>org.apache.log4j.RollingFileAppender</type>
<conversion-pattern>
%-4r [%t] %-5p %c %x - %m%n
</conversion-pattern>
<properties>
<property>
<name>file</name>
<value>${plexus.home}/logs/plexus-rolling.log</value>
</property>
<property>
<name>append</name>
<value>true</value>
</property>
<property>
<name>maxBackupIndex</name>
<value>10</value>
</property>
<property>
<name>maxFileSize</name>
<value>20</value>
</property>
</properties>
</appender>
</appenders>
</configuration>
</component>
</components>
</plexus>