Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvladt2011-02-10 14:16:02 -0500
committervladt2011-02-10 14:17:17 -0500
commit61b17bcf0343faf6b52e1ff811f0ed8cf4abedb9 (patch)
treeeb95444ba76d9be794f0b454f3dc90f026e96427 /org.eclipse.m2e.logback.appender/src
parentb43cfc12b068a597311a343d56ae76f88dc7b1d2 (diff)
downloadm2e-core-61b17bcf0343faf6b52e1ff811f0ed8cf4abedb9.tar.gz
m2e-core-61b17bcf0343faf6b52e1ff811f0ed8cf4abedb9.tar.xz
m2e-core-61b17bcf0343faf6b52e1ff811f0ed8cf4abedb9.zip
335872: Added logback appenders for eclipse log and maven console
Diffstat (limited to 'org.eclipse.m2e.logback.appender/src')
-rw-r--r--org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/EclipseLogAppender.java59
-rw-r--r--org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/MavenConsoleAppender.java60
2 files changed, 119 insertions, 0 deletions
diff --git a/org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/EclipseLogAppender.java b/org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/EclipseLogAppender.java
new file mode 100644
index 00000000..c1ce5dbd
--- /dev/null
+++ b/org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/EclipseLogAppender.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Sonatype, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Sonatype, Inc. - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.m2e.logback.appender;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.UnsynchronizedAppenderBase;
+
+import org.eclipse.core.runtime.ILog;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+
+
+public class EclipseLogAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
+ private static final String ID = "org.eclipse.m2e.logback.appender.EclipseLogAppender"; //$NON-NLS-1$
+
+ @Override
+ protected void append(ILoggingEvent logEvent) {
+ int severity = 0;
+ switch(logEvent.getLevel().levelInt) {
+ case Level.ERROR_INT:
+ severity = IStatus.ERROR;
+ break;
+ case Level.WARN_INT:
+ severity = IStatus.WARNING;
+ break;
+ default:
+ return;
+ }
+
+ IStatus status = new Status(severity, ID, logEvent.getFormattedMessage(), getThrowable(logEvent));
+ ILog eclipseLog = Platform.getLog(null);
+ eclipseLog.log(status);
+ }
+
+ private Throwable getThrowable(ILoggingEvent logEvent) {
+ Object[] args = logEvent.getArgumentArray();
+ if(args == null || args.length == 0) {
+ return null;
+ }
+
+ Object lastObject = args[args.length - 1];
+ if(lastObject instanceof Throwable) {
+ return (Throwable) lastObject;
+ }
+
+ return null;
+ }
+}
diff --git a/org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/MavenConsoleAppender.java b/org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/MavenConsoleAppender.java
new file mode 100644
index 00000000..100e2bb2
--- /dev/null
+++ b/org.eclipse.m2e.logback.appender/src/main/java/org/eclipse/m2e/logback/appender/MavenConsoleAppender.java
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Sonatype, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Sonatype, Inc. - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.m2e.logback.appender;
+
+import org.osgi.framework.Bundle;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.UnsynchronizedAppenderBase;
+
+import org.eclipse.core.runtime.Platform;
+
+import org.eclipse.m2e.core.MavenPlugin;
+import org.eclipse.m2e.core.core.MavenConsole;
+
+
+public class MavenConsoleAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
+ private static final String M2E_CORE_BUNDLE_ID = "org.eclipse.m2e.core"; //$NON-NLS-1$
+
+ private Bundle m2eCoreBundle;
+
+ @Override
+ protected void append(ILoggingEvent logEvent) {
+ if(!isActive()) {
+ return;
+ }
+
+ MavenConsole mavenConsole = MavenPlugin.getDefault().getConsole();
+ if(!mavenConsole.wasInitialized()) {
+ return;
+ }
+
+ if(logEvent.getLevel().levelInt == Level.ERROR_INT) {
+ mavenConsole.logError(logEvent.toString());
+ } else {
+ mavenConsole.logMessage(logEvent.toString());
+ }
+ }
+
+ private boolean isActive() {
+ if(m2eCoreBundle == null) {
+ m2eCoreBundle = Platform.getBundle(M2E_CORE_BUNDLE_ID);
+ if(m2eCoreBundle == null) {
+ System.out.println("Could not find " + M2E_CORE_BUNDLE_ID + " bundle.");
+ return false;
+ }
+ }
+
+ return m2eCoreBundle.getState() == Bundle.ACTIVE;
+ }
+}

Back to the top