2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.log4j
;
19 import java
.util
.Date
;
20 import java
.util
.concurrent
.BlockingQueue
;
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
;
30 /** Not meant to be used directly in standard log4j config */
31 public class SlcExecutionAppender
extends AppenderSkeleton
{
33 private Boolean disabled
= false;
35 private String level
= null;
37 private Level log4jLevel
= null;
39 /** Marker to prevent stack overflow */
40 private ThreadLocal
<Boolean
> dispatching
= new ThreadLocal
<Boolean
>() {
43 protected Boolean
initialValue() {
48 // private Layout layout = null;
49 // private String pattern = "%m - %c%n";
50 private Boolean onlyExecutionThread
= false;
53 // if (layout != null)
56 // setLayout(new PatternLayout(pattern));
57 Logger
.getRootLogger().addAppender(this);
61 protected void append(LoggingEvent event
) {
65 if (dispatching
.get())
68 if (level
!= null && !level
.trim().equals("")) {
69 if (log4jLevel
== null || !log4jLevel
.toString().equals(level
))
71 log4jLevel
= Level
.toLevel(level
);
72 } catch (Exception e
) {
74 .println("Log4j level could not be set for level '"
75 + level
+ "', resetting it to null.");
80 if (log4jLevel
!= null
81 && !event
.getLevel().isGreaterOrEqual(log4jLevel
)) {
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
))
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
;
106 type
= ExecutionStep
.INFO
;
108 ExecutionStep step
= new ExecutionStep(event
.getLoggerName(),
109 new Date(event
.getTimeStamp()), type
, event
.getMessage()
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: "
119 // FIXME understand why it block indefinitely: the queue
120 // should be emptied by the logging thread
125 dispatching
.set(false);
130 public void destroy() throws Exception
{
131 Logger
.getRootLogger().removeAppender(this);
134 public void close() {
137 public boolean requiresLayout() {
141 // public void setLayout(Layout layout) {
142 // this.layout = layout;
145 /** For development purpose, since using regular logging is not easy here */
146 static void stdOut(Object obj
) {
147 System
.out
.println(obj
);
150 // public void setPattern(String pattern) {
151 // this.pattern = pattern;
154 public void setOnlyExecutionThread(Boolean onlyExecutionThread
) {
155 this.onlyExecutionThread
= onlyExecutionThread
;
158 public void setDisabled(Boolean disabled
) {
159 this.disabled
= disabled
;
162 public void setLevel(String level
) {