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
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:
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>