log4j logger question
Hi Nexus team,
I'm implementing a plugin to inspect the artifacts retrival event and log the desirable artifacts/event
information to a log file. I'm able to retrive the artifacts/event information w/o issue. The problem is in the logging portion of my code. I receive "NullPointer" error as shown below from nexus log below:
---------------------------------------------------------------------------------------------------------------------------------------
2009-04-13 21:42:06 INFO [pool-2-thread-2] - o.s.n.p.s.r.RemoteR~:apacheH~ - Remote storage settings change
detect
ed, updating HttpClient...
2009-04-13 21:42:08 WARN [qtp0-0 ] - o.s.n.e.EventInspec~:default - EventInspector
hint='PluginEventInspe
ctor' class='com.cisco.surf.tools.nexus.logger.PluginEventInspector' had problem inspecting an event='class
org.son
atype.nexus.proxy.events.RepositoryItemEventRetrieve'
java.lang.NullPointerException
at com.cisco.surf.tools.nexus.logger.PluginEventInspector.initializeLog(PluginEventInspector.java:134)
at com.cisco.surf.tools.nexus.logger.PluginEventInspector.inspect(PluginEventInspector.java:74)
at org.sonatype.nexus.events.DefaultEventInspectorHost.processEvent(DefaultEventInspectorHost.java:48)
at org.sonatype.nexus.events.DefaultEventInspectorHost.onProximityEvent
(DefaultEventInspectorHost.java:63)
at
---------------------------------------------------------------------------------------------------------------------------------------
Basically, the PluginEvenInspector class extends AbstractEventInspector. This class extracts the
RepositoryItemEventRetrival event and calls the TestLogging class to add additional log4j configuration.
The reason for adding the additional logger "mylogfile" configuration is to support logging contents
of the PluginEventInspector class to the main logger "logfile" (nexus.log) and only selected information such as the artifacts name and requester IP address to the child logger "mylogfile" (artifactsRetrival.log).
---------------------------------------------------------------------------------------------------------------------------------------
public class TestLogging
extends SimpleLog4jConfig
{
private File aLog;
public TestLogging( SimpleLog4jConfig logConfig, File testLog )
{
super( logConfig.getRootLogger(), logConfig.getFileAppenderLocation(),
logConfig.getFileAppenderPattern() );
this.aLog = testLog;
}
@Override
public Map<String, String> toMap()
{
Map<String, String> configs = new LinkedHashMap<String, String>();
configs.put( " key", "value " );
configs.put( "log4j.logger.com.cisco.surf.tools.nexus.logger.PluginEventInspector", "INFO, mylogfile"
);
configs.put( "log4j.appender.mylogfile", "org.apache.log4j.DailyRollingFileAppender" );
configs.put( "log4j.appender.mylogfile.File", aLog.getAbsolutePath() );
configs.put( "log4j.appender.mylogfile.Append", "true" );
configs.put( "log4j.appender.mylogfile.DatePattern", "'.'yyyy-MM-dd" );
configs.put( "log4j.appender.mylogfile.layout", "org.sonatype.nexus.log4j.ConcisePatternLayout" );
configs.put( "log4j.appender.mylogfile.layout.ConversionPattern", "%4d{yyyy-MM-dd HH:mm:ss} %-5p [%-
15.15t] - %c - %m%n" );
return configs;
}
}
---------------------------------------------------------------------------------------------------------------------------------------
public class PluginEventInspector
extends AbstractEventInspector
{
private static String LOG_NAME = "artifactsRetrival.log";
private LogManager logManager;
private Nexus nexus;
public boolean accepts( AbstractEvent evt )
{
return evt instanceof RepositoryItemEventRetrieve;
}
public void inspect( AbstractEvent evt )
{
RepositoryItemEvent repositoryEvent = (RepositoryItemEvent) evt;
StorageItem repositoryItem = repositoryEvent.getItem();
initializeLog();
final Logger log = Logger.getLogger("mylogfile");
log.info("Start logging................");
if (evt instanceof RepositoryItemEventRetrieve)
{
......
}
}
private void initializeLog()
{
File nexusLog = this.logManager.getLogFile("nexus.log");
File myLog = new File(nexusLog.getParentFile(),LOG_NAME);
try {
SimpleLog4jConfig logConfig = new TestLogging(nexus.getLogConfig(), myLog);
logManager.setLogConfig(logConfig);
} catch (IOException e) {
//todo: do nothing for now
}
}
}