]> 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 SSH UI user info
[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 org.apache.log4j.AppenderSkeleton;
20 import org.apache.log4j.Layout;
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.PatternLayout;
23 import org.apache.log4j.spi.LoggingEvent;
24 import org.argeo.slc.core.execution.ExecutionThread;
25 import org.argeo.slc.core.execution.ProcessThreadGroup;
26 import org.argeo.slc.process.SlcExecutionStep;
27 import org.springframework.beans.factory.DisposableBean;
28 import org.springframework.beans.factory.InitializingBean;
29
30 /** Not meant to be used directly in standard log4j config */
31 public class SlcExecutionAppender extends AppenderSkeleton implements
32 InitializingBean, DisposableBean {
33
34 private Layout layout = null;
35 private String pattern = "%m - %c%n";
36 private Boolean onlyExecutionThread = true;
37
38 public void afterPropertiesSet() {
39 if (layout != null)
40 setLayout(layout);
41 else
42 setLayout(new PatternLayout(pattern));
43 Logger.getRootLogger().addAppender(this);
44 }
45
46 @Override
47 protected void append(LoggingEvent event) {
48 Thread currentThread = Thread.currentThread();
49 if (currentThread.getThreadGroup() instanceof ProcessThreadGroup) {
50 if (onlyExecutionThread
51 && !(currentThread instanceof ExecutionThread))
52 return;
53 ((ProcessThreadGroup) currentThread.getThreadGroup())
54 .dispatchAddStep(new SlcExecutionStep(layout.format(event)));
55 }
56 }
57
58 public void destroy() throws Exception {
59 Logger.getRootLogger().removeAppender(this);
60 }
61
62 public void close() {
63 }
64
65 public boolean requiresLayout() {
66 return false;
67 }
68
69 public void setLayout(Layout layout) {
70 this.layout = layout;
71 }
72
73 public void setPattern(String pattern) {
74 this.pattern = pattern;
75 }
76
77 public void setOnlyExecutionThread(Boolean onlyExecutionThread) {
78 this.onlyExecutionThread = onlyExecutionThread;
79 }
80
81 }