Add log4j support
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 27 Apr 2010 15:52:36 +0000 (15:52 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 27 Apr 2010 15:52:36 +0000 (15:52 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@3503 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

modules/agent/org.argeo.slc.agent/META-INF/MANIFEST.MF
modules/agent/org.argeo.slc.agent/META-INF/spring/manager.xml
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/ExecutionThread.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/log4j/SlcExecutionAppender.java [new file with mode: 0644]

index db6d4ed297efc20c8cf332fc92b322e1355a7864..6c1853d516e98f4805325eac0c7a9ebe6adba1db 100644 (file)
@@ -5,4 +5,5 @@ Bundle-Version: 0.12.2.SNAPSHOT
 Bundle-Name: Argeo SLC Agent
 Bundle-SymbolicName: org.argeo.slc.agent
 Import-Package: org.apache.xalan.processor,
- org.apache.xerces.jaxp
+ org.apache.xerces.jaxp,
+ org.argeo.slc.log4j
index 13c93f7e9a309969907e0e0bc0210a45f60b989e..50e9d5aa3231348dda586845c30be1d9ae887071 100644 (file)
@@ -12,4 +12,5 @@
 \r
        <bean id="bundlesManager" class="org.argeo.slc.osgi.BundlesManager" />\r
 \r
+       <bean class="org.argeo.slc.log4j.SlcExecutionAppender" />\r
 </beans>
\ No newline at end of file
index f1ebde364142d7db5cfa91085b7750d9ac208cd8..c81e8b1a0d64eecfa2b13bd5a2a3349d1024ed2a 100644 (file)
@@ -8,7 +8,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.process.RealizedFlow;
-import org.argeo.slc.process.SlcExecution;
 import org.argeo.slc.process.SlcExecutionNotifier;
 import org.argeo.slc.process.SlcExecutionStep;
 
@@ -41,8 +40,8 @@ public class ExecutionThread extends Thread {
                                .getFlowDescriptor();
                String flowName = executionFlowDescriptor.getName();
 
-               dispatchAddStep(processThread.getSlcProcess(), new SlcExecutionStep(
-                               SlcExecutionStep.TYPE_PHASE_START, "Flow " + flowName));
+               dispatchAddStep(new SlcExecutionStep(SlcExecutionStep.TYPE_PHASE_START,
+                               "Flow " + flowName));
 
                try {
                        String autoUpgrade = System
@@ -55,26 +54,24 @@ public class ExecutionThread extends Thread {
                        // TODO: re-throw exception ?
                        String msg = "Execution of flow " + flowName + " failed.";
                        log.error(msg, e);
-                       dispatchAddStep(processThread.getSlcProcess(),
-                                       new SlcExecutionStep(msg + " " + e.getMessage()));
+                       dispatchAddStep(new SlcExecutionStep(msg + " " + e.getMessage()));
                        processThread.notifyError();
                } finally {
                        processThread.flowCompleted();
-                       dispatchAddStep(processThread.getSlcProcess(),
-                                       new SlcExecutionStep(SlcExecutionStep.TYPE_PHASE_END,
-                                                       "Flow " + flowName));
+                       dispatchAddStep(new SlcExecutionStep(
+                                       SlcExecutionStep.TYPE_PHASE_END, "Flow " + flowName));
                }
        }
 
-       protected void dispatchAddStep(SlcExecution slcExecution,
-                       SlcExecutionStep step) {
-               slcExecution.getSteps().add(step);
+       public void dispatchAddStep(SlcExecutionStep step) {
+               processThread.getSlcProcess().getSteps().add(step);
                List<SlcExecutionStep> steps = new ArrayList<SlcExecutionStep>();
                steps.add(step);
                for (Iterator<SlcExecutionNotifier> it = processThread
                                .getExecutionModulesManager().getSlcExecutionNotifiers()
                                .iterator(); it.hasNext();) {
-                       it.next().addSteps(slcExecution, steps);
+                       it.next().addSteps(processThread.getSlcProcess(), steps);
                }
        }
+
 }
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/log4j/SlcExecutionAppender.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/log4j/SlcExecutionAppender.java
new file mode 100644 (file)
index 0000000..85d3e67
--- /dev/null
@@ -0,0 +1,58 @@
+package org.argeo.slc.log4j;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.Layout;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.spi.LoggingEvent;
+import org.argeo.slc.core.execution.ExecutionThread;
+import org.argeo.slc.process.SlcExecutionStep;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
+
+/** Not meant to be used directly in standard log4j config */
+public class SlcExecutionAppender extends AppenderSkeleton implements
+               InitializingBean, DisposableBean {
+
+       private Layout layout = null;
+       private String pattern = "%m - %c%n";
+
+       public void afterPropertiesSet() {
+               if (layout != null)
+                       setLayout(layout);
+               else
+                       setLayout(new PatternLayout(pattern));
+               Logger.getRootLogger().addAppender(this);
+       }
+
+       @Override
+       protected void append(LoggingEvent event) {
+               if (!(Thread.currentThread() instanceof ExecutionThread))
+                       return;
+
+               ExecutionThread executionThread = (ExecutionThread) Thread
+                               .currentThread();
+               executionThread.dispatchAddStep(new SlcExecutionStep(layout
+                               .format(event)));
+       }
+
+       public void destroy() throws Exception {
+               Logger.getRootLogger().removeAppender(this);
+       }
+
+       public void close() {
+       }
+
+       public boolean requiresLayout() {
+               return false;
+       }
+
+       public void setLayout(Layout layout) {
+               this.layout = layout;
+       }
+
+       public void setPattern(String pattern) {
+               this.pattern = pattern;
+       }
+
+}