]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/log4j/SlcExecutionAppender.java
Improve logging
[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.execution.ExecutionStep;
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 private Boolean disabled = false;
38
39 private String level = null;
40
41 private Level log4jLevel = null;
42
43 /** Marker to prevent stack overflow */
44 private ThreadLocal<Boolean> dispatching = new ThreadLocal<Boolean>() {
45
46 @Override
47 protected Boolean initialValue() {
48 return false;
49 }
50 };
51
52 private Layout layout = null;
53 private String pattern = "%m - %c%n";
54 private Boolean onlyExecutionThread = false;
55
56 public void afterPropertiesSet() {
57 if (layout != null)
58 setLayout(layout);
59 else
60 setLayout(new PatternLayout(pattern));
61 Logger.getRootLogger().addAppender(this);
62 }
63
64 @Override
65 protected void append(LoggingEvent event) {
66 if (disabled)
67 return;
68
69 if (dispatching.get())
70 return;
71
72 if (level != null && !level.trim().equals("")) {
73 if (log4jLevel == null || !log4jLevel.toString().equals(level))
74 try {
75 log4jLevel = Level.toLevel(level);
76 } catch (Exception e) {
77 System.err
78 .println("Log4j level could not be set for level '"
79 + level + "', resetting it to null.");
80 e.printStackTrace();
81 level = null;
82 }
83
84 if (log4jLevel != null
85 && !event.getLevel().isGreaterOrEqual(log4jLevel)) {
86 return;
87 }
88 }
89
90 Thread currentThread = Thread.currentThread();
91 if (currentThread.getThreadGroup() instanceof ProcessThreadGroup) {
92 if (onlyExecutionThread
93 && !(currentThread instanceof ExecutionThread))
94 return;
95
96 final String type;
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;
108 else
109 type = ExecutionStep.INFO;
110
111 ExecutionStep step = new ExecutionStep(new Date(
112 event.getTimeStamp()), type, layout.format(event));
113
114 try {
115 dispatching.set(true);
116 ((ProcessThreadGroup) currentThread.getThreadGroup())
117 .dispatchAddStep(step);
118 } finally {
119 dispatching.set(false);
120 }
121 }
122 }
123
124 public void destroy() throws Exception {
125 Logger.getRootLogger().removeAppender(this);
126 }
127
128 public void close() {
129 }
130
131 public boolean requiresLayout() {
132 return false;
133 }
134
135 public void setLayout(Layout layout) {
136 this.layout = layout;
137 }
138
139 public void setPattern(String pattern) {
140 this.pattern = pattern;
141 }
142
143 public void setOnlyExecutionThread(Boolean onlyExecutionThread) {
144 this.onlyExecutionThread = onlyExecutionThread;
145 }
146
147 public void setDisabled(Boolean disabled) {
148 this.disabled = disabled;
149 }
150
151 public void setLevel(String level) {
152 this.level = level;
153 }
154
155 }