]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/log4j/SlcExecutionAppender.java
997b6d04510e62e6ffa1f5c4ed2f8e22b6819996
[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.process.SlcExecutionStep;
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 Layout layout = null;
38 private String pattern = "%m - %c%n";
39 private Boolean onlyExecutionThread = true;
40
41 public void afterPropertiesSet() {
42 if (layout != null)
43 setLayout(layout);
44 else
45 setLayout(new PatternLayout(pattern));
46 Logger.getRootLogger().addAppender(this);
47 }
48
49 @Override
50 protected void append(LoggingEvent event) {
51 Thread currentThread = Thread.currentThread();
52 if (currentThread.getThreadGroup() instanceof ProcessThreadGroup) {
53 if (onlyExecutionThread
54 && !(currentThread instanceof ExecutionThread))
55 return;
56
57 final String type;
58 if (event.getLevel().equals(Level.ERROR)
59 || event.getLevel().equals(Level.FATAL))
60 type = SlcExecutionStep.ERROR;
61 else if (event.getLevel().equals(Level.WARN))
62 type = SlcExecutionStep.WARNING;
63 else if (event.getLevel().equals(Level.INFO))
64 type = SlcExecutionStep.INFO;
65 else if (event.getLevel().equals(Level.DEBUG))
66 type = SlcExecutionStep.DEBUG;
67 else if (event.getLevel().equals(Level.TRACE))
68 type = SlcExecutionStep.TRACE;
69 else
70 type = SlcExecutionStep.INFO;
71
72 SlcExecutionStep step = new SlcExecutionStep(new Date(event
73 .getTimeStamp()), type, layout.format(event));
74
75 ((ProcessThreadGroup) currentThread.getThreadGroup())
76 .dispatchAddStep(step);
77 }
78 }
79
80 public void destroy() throws Exception {
81 Logger.getRootLogger().removeAppender(this);
82 }
83
84 public void close() {
85 }
86
87 public boolean requiresLayout() {
88 return false;
89 }
90
91 public void setLayout(Layout layout) {
92 this.layout = layout;
93 }
94
95 public void setPattern(String pattern) {
96 this.pattern = pattern;
97 }
98
99 public void setOnlyExecutionThread(Boolean onlyExecutionThread) {
100 this.onlyExecutionThread = onlyExecutionThread;
101 }
102
103 }