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
;
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
.execution
.ExecutionStep
;
30 import org
.springframework
.beans
.factory
.DisposableBean
;
31 import org
.springframework
.beans
.factory
.InitializingBean
;
33 /** Not meant to be used directly in standard log4j config */
34 public class SlcExecutionAppender
extends AppenderSkeleton
implements
35 InitializingBean
, DisposableBean
{
37 private Boolean disabled
= false;
39 private String level
= null;
41 private Level log4jLevel
= null;
43 /** Marker to prevent stack overflow */
44 private ThreadLocal
<Boolean
> dispatching
= new ThreadLocal
<Boolean
>() {
47 protected Boolean
initialValue() {
52 private Layout layout
= null;
53 private String pattern
= "%m - %c%n";
54 private Boolean onlyExecutionThread
= false;
56 public void afterPropertiesSet() {
60 setLayout(new PatternLayout(pattern
));
61 Logger
.getRootLogger().addAppender(this);
65 protected void append(LoggingEvent event
) {
69 if (dispatching
.get())
72 if (level
!= null && !level
.trim().equals("")) {
73 if (log4jLevel
== null || !log4jLevel
.toString().equals(level
))
75 log4jLevel
= Level
.toLevel(level
);
76 } catch (Exception e
) {
78 .println("Log4j level could not be set for level '"
79 + level
+ "', resetting it to null.");
84 if (log4jLevel
!= null
85 && !event
.getLevel().isGreaterOrEqual(log4jLevel
)) {
90 Thread currentThread
= Thread
.currentThread();
91 if (currentThread
.getThreadGroup() instanceof ProcessThreadGroup
) {
92 if (onlyExecutionThread
93 && !(currentThread
instanceof ExecutionThread
))
97 if (event
.getLevel().equals(Level
.ERROR
)
98 || event
.getLevel().equals(Level
.FATAL
))
99 type
= ExecutionStep
.ERROR
;
100 else if (event
.getLevel().equals(Level
.WARN
))
101 type
= ExecutionStep
.WARNING
;
102 else if (event
.getLevel().equals(Level
.INFO
))
103 type
= ExecutionStep
.INFO
;
104 else if (event
.getLevel().equals(Level
.DEBUG
))
105 type
= ExecutionStep
.DEBUG
;
106 else if (event
.getLevel().equals(Level
.TRACE
))
107 type
= ExecutionStep
.TRACE
;
109 type
= ExecutionStep
.INFO
;
111 ExecutionStep step
= new ExecutionStep(new Date(
112 event
.getTimeStamp()), type
, layout
.format(event
));
115 dispatching
.set(true);
116 ((ProcessThreadGroup
) currentThread
.getThreadGroup())
117 .dispatchAddStep(step
);
119 dispatching
.set(false);
124 public void destroy() throws Exception
{
125 Logger
.getRootLogger().removeAppender(this);
128 public void close() {
131 public boolean requiresLayout() {
135 public void setLayout(Layout layout
) {
136 this.layout
= layout
;
139 public void setPattern(String pattern
) {
140 this.pattern
= pattern
;
143 public void setOnlyExecutionThread(Boolean onlyExecutionThread
) {
144 this.onlyExecutionThread
= onlyExecutionThread
;
147 public void setDisabled(Boolean disabled
) {
148 this.disabled
= disabled
;
151 public void setLevel(String level
) {