2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.log4j
;
18 import java
.util
.Date
;
19 import java
.util
.concurrent
.BlockingQueue
;
21 import org
.apache
.log4j
.AppenderSkeleton
;
22 import org
.apache
.log4j
.Level
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.apache
.log4j
.spi
.LoggingEvent
;
25 import org
.argeo
.slc
.core
.execution
.ExecutionThread
;
26 import org
.argeo
.slc
.core
.execution
.ProcessThreadGroup
;
27 import org
.argeo
.slc
.execution
.ExecutionStep
;
29 /** Not meant to be used directly in standard log4j config */
30 public class SlcExecutionAppender
extends AppenderSkeleton
{
32 private Boolean disabled
= false;
34 private String level
= null;
36 private Level log4jLevel
= null;
38 /** Marker to prevent stack overflow */
39 private ThreadLocal
<Boolean
> dispatching
= new ThreadLocal
<Boolean
>() {
42 protected Boolean
initialValue() {
47 // private Layout layout = null;
48 // private String pattern = "%m - %c%n";
49 private Boolean onlyExecutionThread
= false;
52 // if (layout != null)
55 // setLayout(new PatternLayout(pattern));
56 Logger
.getRootLogger().addAppender(this);
60 protected void append(LoggingEvent event
) {
64 if (dispatching
.get())
67 if (level
!= null && !level
.trim().equals("")) {
68 if (log4jLevel
== null || !log4jLevel
.toString().equals(level
))
70 log4jLevel
= Level
.toLevel(level
);
71 } catch (Exception e
) {
73 .println("Log4j level could not be set for level '"
74 + level
+ "', resetting it to null.");
79 if (log4jLevel
!= null
80 && !event
.getLevel().isGreaterOrEqual(log4jLevel
)) {
85 // Check whether we are within an executing process
86 Thread currentThread
= Thread
.currentThread();
87 if (currentThread
.getThreadGroup() instanceof ProcessThreadGroup
) {
88 if (onlyExecutionThread
89 && !(currentThread
instanceof ExecutionThread
))
93 if (event
.getLevel().equals(Level
.ERROR
)
94 || event
.getLevel().equals(Level
.FATAL
))
95 type
= ExecutionStep
.ERROR
;
96 else if (event
.getLevel().equals(Level
.WARN
))
97 type
= ExecutionStep
.WARNING
;
98 else if (event
.getLevel().equals(Level
.INFO
))
99 type
= ExecutionStep
.INFO
;
100 else if (event
.getLevel().equals(Level
.DEBUG
))
101 type
= ExecutionStep
.DEBUG
;
102 else if (event
.getLevel().equals(Level
.TRACE
))
103 type
= ExecutionStep
.TRACE
;
105 type
= ExecutionStep
.INFO
;
107 ExecutionStep step
= new ExecutionStep(event
.getLoggerName(),
108 new Date(event
.getTimeStamp()), type
, event
.getMessage()
112 dispatching
.set(true);
113 BlockingQueue
<ExecutionStep
> steps
= ((ProcessThreadGroup
) currentThread
114 .getThreadGroup()).getSteps();
115 if (steps
.remainingCapacity() == 0) {
116 stdOut("WARNING: execution steps queue is full, skipping step: "
118 // FIXME understand why it block indefinitely: the queue
119 // should be emptied by the logging thread
124 dispatching
.set(false);
129 public void destroy() throws Exception
{
130 Logger
.getRootLogger().removeAppender(this);
133 public void close() {
136 public boolean requiresLayout() {
140 // public void setLayout(Layout layout) {
141 // this.layout = layout;
144 /** For development purpose, since using regular logging is not easy here */
145 static void stdOut(Object obj
) {
146 System
.out
.println(obj
);
149 // public void setPattern(String pattern) {
150 // this.pattern = pattern;
153 public void setOnlyExecutionThread(Boolean onlyExecutionThread
) {
154 this.onlyExecutionThread
= onlyExecutionThread
;
157 public void setDisabled(Boolean disabled
) {
158 this.disabled
= disabled
;
161 public void setLevel(String level
) {