Source code: de/danet/an/util/log4j/ListAppender.java
1 /*
2 * This file is part of the WfMCore/WfMOpen project.
3 * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * $Id: ListAppender.java,v 1.4 2003/06/27 08:51:46 lipp Exp $
21 *
22 * $Log: ListAppender.java,v $
23 * Revision 1.4 2003/06/27 08:51:46 lipp
24 * Fixed copyright/license information.
25 *
26 * Revision 1.3 2002/12/04 10:27:40 huaiyang
27 * Level of the UI Message as string.
28 *
29 * Revision 1.2 2002/11/29 12:46:45 huaiyang
30 * Modified the inner class of UIMessage.
31 *
32 * Revision 1.1 2002/11/29 09:44:59 lipp
33 * New UI logging design.
34 *
35 */
36 package de.danet.an.util.log4j;
37
38 import java.util.List;
39
40 import java.lang.ref.WeakReference;
41
42 import org.apache.log4j.AppenderSkeleton;
43 import org.apache.log4j.spi.LoggingEvent;
44
45 /**
46 * This class provides an appender that adds log messages to a list
47 * found in the MDC.
48 *
49 * @author <a href="mailto:lipp@danet.de"></a>
50 * @version $Revision: 1.4 $
51 */
52
53 public class ListAppender extends AppenderSkeleton {
54
55 private static final org.apache.log4j.Category logger
56 = ApplLogger.getLogger (ListAppender.class);
57
58 /**
59 * Unter this key is the appended messages saved in the MDC.
60 */
61 public static final String MDC_LIST_KEY
62 = "de.danet.an.util.log4j.List";
63
64 /**
65 * This class defines the log message sent to user interface.
66 */
67 public static class UIMessage implements Comparable {
68 private int levelInt;
69 private String level;
70 private String message;
71
72 /**
73 * Default constructor.
74 * @param loggingEvent the given event to be parsed in message.
75 */
76 public UIMessage (LoggingEvent loggingEvent) {
77 level = loggingEvent.level.toString();
78 levelInt = loggingEvent.level.toInt();
79 message = loggingEvent.getRenderedMessage();
80 }
81 /**
82 * return the level of this message.
83 * @return the level of this message.
84 */
85 public String level() {
86 return level;
87 }
88
89 /**
90 * return the message.
91 * @return the message.
92 */
93 public String message() {
94 return message;
95 }
96
97 /**
98 * compare message.
99 * @param oMessage the message to be compared.
100 * @return compare message.
101 */
102 public int compareTo(Object oMessage) {
103 UIMessage message = (UIMessage)oMessage;
104 return levelInt - message.levelInt;
105 }
106 }
107
108 /**
109 * Creates an instance of <code>ListAppender</code>
110 * with all attributes initialized to default values.
111 */
112 public ListAppender () {
113 super();
114 }
115
116 /**
117 * Forwards the message to the EJBSink bean.
118 *
119 * @param event the event including the message to be logged.
120 */
121 public void append(LoggingEvent event) {
122 List msgList = null;
123 if (event.getMDC(MDC_LIST_KEY) != null) {
124 msgList = (List)((WeakReference)event.getMDC(MDC_LIST_KEY)).get();
125 }
126 if (msgList == null) {
127 logger.error("Environment for appender not initialized!");
128 return;
129 }
130 logger.info("MessageList addMessage: " + event.getMessage());
131 msgList.add (new UIMessage(event));
132 }
133
134 /**
135 * Internal method. Close the database connection and flush the buffer.
136 */
137 public void close() {
138 }
139
140 /**
141 * Internal method. Returns true, you may define your own layout...
142 *
143 *@return in this version allways <code>true</code>
144 */
145 public boolean requiresLayout() {
146 return true;
147 }
148
149 }