NEXUS-5118: Rationalizing logging in bundle #530

Merged
merged 7 commits into from Sep 14, 2012
+245 −7
Split
@@ -0,0 +1,42 @@
+<!--
+
+ Sonatype Nexus (TM) Open Source Version
+ Copyright (c) 2007-2012 Sonatype, Inc.
+ All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+
+ This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+
+ Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ Eclipse Foundation. All other trademarks are the property of their respective owners.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus</artifactId>
+ <version>2.2-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>nexus-logging-extras-appender</artifactId>
+ <name>Nexus : Logging Extras - Appender</name>
+
+ <dependencies>
+ <!-- Logging -->
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+</project>
@@ -0,0 +1,26 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.logback;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+
+/**
+ * An target that receives logging events from {@link ForwardingAppender}.
+ *
+ * @author cstamas
+ * @since 2.2
+ */
+public interface EventTarget
@peterlynch
peterlynch Sep 4, 2012 Member

If this is all about logging, is there a reason you did not call this LoggingEventTarget?

+{
+ void onEvent( ILoggingEvent event );
+}
@@ -0,0 +1,70 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.logback;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.UnsynchronizedAppenderBase;
+
+/**
+ * A forwarding appender for Logback, that forwards {@link ILoggingEvent} events it gets as Appender, to it's target.
+ *
+ * @author cstamas
+ * @since 2.2
+ */
+public class ForwardingAppender
+ extends UnsynchronizedAppenderBase<ILoggingEvent>
+{
+ private EventTarget target;
+
+ /**
+ * Default constructor that creates and resets this instance.
+ */
+ public ForwardingAppender()
+ {
+ reset();
+ }
+
+ @Override
+ protected void append( final ILoggingEvent event )
+ {
+ target.onEvent( event );
+ }
+
+ // ==
+
+ /**
+ * Installs or replaces an {@link EventTarget} to have events forwarded to it.
+ *
+ * @param target
+ */
+ public void installEventTarget( final EventTarget target )
+ {
+ if ( target != null )
+ {
+ this.target = target;
+ }
+ else
+ {
+ reset();
+ }
+ }
+
+ /**
+ * Performs a "reset" of this forwarding appender, and previously installed {@link EventTarget} (if any) will stop
+ * receiving events.
+ */
+ public void reset()
+ {
+ this.target = NoopEventTarget.INSTANCE;
+ }
+}
@@ -0,0 +1,27 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.logback;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+
+public final class NoopEventTarget
+ implements EventTarget
+{
+ final static NoopEventTarget INSTANCE = new NoopEventTarget();
+
+ @Override
+ public void onEvent( final ILoggingEvent event )
+ {
+ // noop
+ }
+}
@@ -26,6 +26,12 @@
<name>Nexus : Logging Extras</name>
<dependencies>
+ <!-- The appender -->
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-logging-extras-appender</artifactId>
+ </dependency>
+
<!-- We have components in here -->
<dependency>
<groupId>org.sonatype.sisu</groupId>
@@ -50,6 +50,8 @@
import org.sonatype.nexus.log.LogConfiguration;
import org.sonatype.nexus.log.LogConfigurationParticipant;
import org.sonatype.nexus.log.LogManager;
+import org.sonatype.nexus.logback.EventTarget;
+import org.sonatype.nexus.logback.ForwardingAppender;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
@@ -101,6 +103,9 @@
@Requirement
private ApplicationConfiguration applicationConfiguration;
+ @Requirement
+ private EventTarget eventTarget;
+
private ObjectName jmxName;
public LogbackLogManager()
@@ -482,6 +487,15 @@ private void injectAppenders()
{
Appender<ILoggingEvent> ap = it.next();
injector.injectMembers( ap );
+
+ // ForwardingAppender needs some special care: manual injection
+ // since @Inject is not on Jetty classpath (parent of this classpath) and Injector will not do anything
+ // with it since classloader will not pick up annotations on class while loading up the class itself
+ if ( ap instanceof ForwardingAppender )
+ {
+ final ForwardingAppender fap = (ForwardingAppender) ap;
+ fap.installEventTarget( eventTarget );
+ }
}
}
}
@@ -0,0 +1,44 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.log.internal;
+
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.sonatype.nexus.logback.EventTarget;
+import org.sonatype.plexus.appevents.ApplicationEventMulticaster;
+
+import ch.qos.logback.classic.spi.ILoggingEvent;
+
+/**
+ * {@link EventTarget} that will multicast the incoming logging event to {@link ApplicationEventMulticaster}.
+ *
+ * @author cstamas
+ * @since 2.2
+ */
+@Component( role = EventTarget.class )
+public class NexusEventSystemEventTarget
+ implements EventTarget
+{
+ @Requirement
+ private ApplicationEventMulticaster eventMulticaster;
+
+ @Override
+ public void onEvent( final ILoggingEvent eventObject )
+ {
+ if ( eventMulticaster != null )
+ {
+ final LogbackLoggingEvent logEvent = new LogbackLoggingEvent( eventObject );
+ eventMulticaster.notifyEventListeners( logEvent );
+ }
+ }
+}
@@ -19,7 +19,7 @@
<included>
- <appender name="events" class="org.sonatype.nexus.log.internal.NexusEventSystemAppender">
+ <appender name="events" class="org.sonatype.nexus.logback.ForwardingAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
@@ -65,6 +65,10 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-logging-extras-appender</artifactId>
+ </dependency>
<!-- The Jetty itself -->
<dependency>
@@ -91,6 +91,10 @@
<excludes>
<exclude>META-INF/**</exclude>
<exclude>WEB-INF/lib/appcontext*.jar</exclude>
+ <exclude>WEB-INF/lib/slf4j-api*.jar</exclude>
+ <exclude>WEB-INF/lib/logback-core*.jar</exclude>
+ <exclude>WEB-INF/lib/logback-classic*.jar</exclude>
+ <exclude>WEB-INF/lib/nexus-logging-extras-appender*.jar</exclude>
<exclude>WEB-INF/classes/logback.xml</exclude>
</excludes>
</unpackOptions>
@@ -101,6 +105,7 @@
<dependencySet>
<includes>
<include>org.sonatype.nexus:nexus-bootstrap</include>
+ <include>org.sonatype.nexus:nexus-logging-extras-appender</include>
<include>javax.servlet:*</include>
<include>org.eclipse.jetty:*</include>
<include>org.codehaus.plexus:*</include>
@@ -51,12 +51,6 @@
<Arg><Ref id="Contexts"/></Arg>
<Arg>${nexus-webapp}</Arg>
<Arg>${nexus-webapp-context-path}</Arg>
- <Set name="serverClasses">
- <Array type="java.lang.String">
- <Item>org.slf4j.</Item>
- <Item>ch.qos.logback.</Item>
- </Array>
- </Set>
<Set name="extractWAR">false</Set>
<Set name="throwUnavailableOnStartupException">true</Set>
</New>
View
@@ -1113,6 +1113,11 @@
</dependency>
<dependency>
<groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-logging-extras-appender</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
<artifactId>nexus-api</artifactId>
<version>${project.version}</version>
</dependency>
@@ -1803,6 +1808,7 @@
<module>nexus-utils</module>
<module>nexus-web-utils</module>
<module>nexus-logging-extras</module>
+ <module>nexus-logging-extras-appender</module>
<module>nexus-api</module>
<module>nexus-oss-edition</module>
<module>nexus-configuration-model</module>