]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/log4j/SlcExecutionAppender.java
Finalize JMS serialization
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / log4j / SlcExecutionAppender.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.log4j;
18
19 import java.util.Date;
20
21 import org.apache.log4j.AppenderSkeleton;
22 import org.apache.log4j.Layout;
23 import org.apache.log4j.Level;
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.PatternLayout;
26 import org.apache.log4j.spi.LoggingEvent;
27 import org.argeo.slc.core.execution.ExecutionThread;
28 import org.argeo.slc.core.execution.ProcessThreadGroup;
29 import org.argeo.slc.process.SlcExecutionStep;
30 import org.springframework.beans.factory.DisposableBean;
31 import org.springframework.beans.factory.InitializingBean;
32
33 /** Not meant to be used directly in standard log4j config */
34 public class SlcExecutionAppender extends AppenderSkeleton implements
35 InitializingBean, DisposableBean {
36
37 /** Marker to prevent stack overflow */
38 private ThreadLocal<Boolean> dispatching = new ThreadLocal<Boolean>() {
39
40 @Override
41 protected Boolean initialValue() {
42 return false;
43 }
44 };
45
46 private Layout layout = null;
47 private String pattern = "%m - %c%n";
48 private Boolean onlyExecutionThread = false;
49
50 public void afterPropertiesSet() {
51 if (layout != null)
52 setLayout(layout);
53 else
54 setLayout(new PatternLayout(pattern));
55 Logger.getRootLogger().addAppender(this);
56 }
57
58 @Override
59 protected void append(LoggingEvent event) {
60 if (dispatching.get())
61 return;
62
63 Thread currentThread = Thread.currentThread();
64 if (currentThread.getThreadGroup() instanceof ProcessThreadGroup) {
65 if (onlyExecutionThread
66 && !(currentThread instanceof ExecutionThread))
67 return;
68
69 final String type;
70 if (event.getLevel().equals(Level.ERROR)
71 || event.getLevel().equals(Level.FATAL))
72 type = SlcExecutionStep.ERROR;
73 else if (event.getLevel().equals(Level.WARN))
74 type = SlcExecutionStep.WARNING;
75 else if (event.getLevel().equals(Level.INFO))
76 type = SlcExecutionStep.INFO;
77 else if (event.getLevel().equals(Level.DEBUG))
78 type = SlcExecutionStep.DEBUG;
79 else if (event.getLevel().equals(Level.TRACE))
80 type = SlcExecutionStep.TRACE;
81 else
82 type = SlcExecutionStep.INFO;
83
84 SlcExecutionStep step = new SlcExecutionStep(new Date(event
85 .getTimeStamp()), type, layout.format(event));
86
87 try {
88 dispatching.set(true);
89 ((ProcessThreadGroup) currentThread.getThreadGroup())
90 .dispatchAddStep(step);
91 } finally {
92 dispatching.set(false);
93 }
94 }
95 }
96
97 public void destroy() throws Exception {
98 Logger.getRootLogger().removeAppender(this);
99 }
100
101 public void close() {
102 }
103
104 public boolean requiresLayout() {
105 return false;
106 }
107
108 public void setLayout(Layout layout) {
109 this.layout = layout;
110 }
111
112 public void setPattern(String pattern) {
113 this.pattern = pattern;
114 }
115
116 public void setOnlyExecutionThread(Boolean onlyExecutionThread) {
117 this.onlyExecutionThread = onlyExecutionThread;
118 }
119
120 }