]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/log4j/SlcExecutionAppender.java
Move Swing JSCH UI in a separate package
[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 import java.util.concurrent.BlockingQueue;
21
22 import org.apache.log4j.AppenderSkeleton;
23 import org.apache.log4j.Level;
24 import org.apache.log4j.Logger;
25 import org.apache.log4j.spi.LoggingEvent;
26 import org.argeo.slc.core.execution.ExecutionThread;
27 import org.argeo.slc.core.execution.ProcessThreadGroup;
28 import org.argeo.slc.execution.ExecutionStep;
29
30 /** Not meant to be used directly in standard log4j config */
31 public class SlcExecutionAppender extends AppenderSkeleton {
32
33 private Boolean disabled = false;
34
35 private String level = null;
36
37 private Level log4jLevel = null;
38
39 /** Marker to prevent stack overflow */
40 private ThreadLocal<Boolean> dispatching = new ThreadLocal<Boolean>() {
41
42 @Override
43 protected Boolean initialValue() {
44 return false;
45 }
46 };
47
48 // private Layout layout = null;
49 // private String pattern = "%m - %c%n";
50 private Boolean onlyExecutionThread = false;
51
52 public void init() {
53 // if (layout != null)
54 // setLayout(layout);
55 // else
56 // setLayout(new PatternLayout(pattern));
57 Logger.getRootLogger().addAppender(this);
58 }
59
60 @Override
61 protected void append(LoggingEvent event) {
62 if (disabled)
63 return;
64
65 if (dispatching.get())
66 return;
67
68 if (level != null && !level.trim().equals("")) {
69 if (log4jLevel == null || !log4jLevel.toString().equals(level))
70 try {
71 log4jLevel = Level.toLevel(level);
72 } catch (Exception e) {
73 System.err
74 .println("Log4j level could not be set for level '"
75 + level + "', resetting it to null.");
76 e.printStackTrace();
77 level = null;
78 }
79
80 if (log4jLevel != null
81 && !event.getLevel().isGreaterOrEqual(log4jLevel)) {
82 return;
83 }
84 }
85
86 // Check whether we are within an executing process
87 Thread currentThread = Thread.currentThread();
88 if (currentThread.getThreadGroup() instanceof ProcessThreadGroup) {
89 if (onlyExecutionThread
90 && !(currentThread instanceof ExecutionThread))
91 return;
92
93 final String type;
94 if (event.getLevel().equals(Level.ERROR)
95 || event.getLevel().equals(Level.FATAL))
96 type = ExecutionStep.ERROR;
97 else if (event.getLevel().equals(Level.WARN))
98 type = ExecutionStep.WARNING;
99 else if (event.getLevel().equals(Level.INFO))
100 type = ExecutionStep.INFO;
101 else if (event.getLevel().equals(Level.DEBUG))
102 type = ExecutionStep.DEBUG;
103 else if (event.getLevel().equals(Level.TRACE))
104 type = ExecutionStep.TRACE;
105 else
106 type = ExecutionStep.INFO;
107
108 ExecutionStep step = new ExecutionStep(event.getLoggerName(),
109 new Date(event.getTimeStamp()), type, event.getMessage()
110 .toString());
111
112 try {
113 dispatching.set(true);
114 BlockingQueue<ExecutionStep> steps = ((ProcessThreadGroup) currentThread
115 .getThreadGroup()).getSteps();
116 if (steps.remainingCapacity() == 0) {
117 stdOut("WARNING: execution steps queue is full, skipping step: "
118 + step);
119 // FIXME understand why it block indefinitely: the queue
120 // should be emptied by the logging thread
121 } else {
122 steps.add(step);
123 }
124 } finally {
125 dispatching.set(false);
126 }
127 }
128 }
129
130 public void destroy() throws Exception {
131 Logger.getRootLogger().removeAppender(this);
132 }
133
134 public void close() {
135 }
136
137 public boolean requiresLayout() {
138 return false;
139 }
140
141 // public void setLayout(Layout layout) {
142 // this.layout = layout;
143 // }
144
145 /** For development purpose, since using regular logging is not easy here */
146 static void stdOut(Object obj) {
147 System.out.println(obj);
148 }
149
150 // public void setPattern(String pattern) {
151 // this.pattern = pattern;
152 // }
153
154 public void setOnlyExecutionThread(Boolean onlyExecutionThread) {
155 this.onlyExecutionThread = onlyExecutionThread;
156 }
157
158 public void setDisabled(Boolean disabled) {
159 this.disabled = disabled;
160 }
161
162 public void setLevel(String level) {
163 this.level = level;
164 }
165
166 }