KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4j > ConsoleHandler


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.util.monolog.wrapper.log4j;
20
21 import org.objectweb.util.monolog.api.Handler;
22 import org.objectweb.util.monolog.api.MonologFactory;
23 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
24 import org.apache.log4j.PatternLayout;
25 import org.apache.log4j.ConsoleAppender;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30
31 /**
32  *
33  * @author Sebastien Chassande-Barrioz
34  */

35 public class ConsoleHandler extends ConsoleAppender implements Handler {
36
37     /**
38      * This fields contains the properties of the Handler
39      */

40     protected HashMap JavaDoc prop = null;
41
42     public ConsoleHandler() {
43         super();
44     }
45
46     /**
47      * It Builds a new ConsoleHandler.
48      * @param name is the handler name.
49      */

50     public ConsoleHandler(String JavaDoc name) {
51         super();
52         setName(name);
53         prop = new HashMap JavaDoc();
54     }
55
56     public Map JavaDoc getAttributes() {
57         return prop;
58     }
59
60     public void setAttributes(Map JavaDoc attributes) {
61         prop.clear();
62         prop.putAll(attributes);
63         Object JavaDoc mf = prop.get("activation");
64         if (mf != null) {
65             prop.remove("activation");
66             setAttribute("activation", mf);
67         }
68     }
69
70     // IMPLEMENTATION OF THE Handler INTERFACE //
71
//---------------------------------------------//
72

73     public String JavaDoc getType() {
74         return "console";
75     }
76
77     public String JavaDoc[] getAttributeNames() {
78         return (String JavaDoc[]) prop.keySet().toArray(new String JavaDoc[0]);
79     }
80
81     public Object JavaDoc getAttribute(String JavaDoc key) {
82         return prop.get(key);
83     }
84
85     public Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value) {
86         if (prop == null)
87             prop = new HashMap JavaDoc();
88         if (!key.equalsIgnoreCase("activation")) {
89             return prop.put(key, value);
90         } else if (prop.containsKey(key)) {
91             return null; //already activated
92
}
93         MonologFactory mf = (MonologFactory) value;
94         String JavaDoc pattern = (String JavaDoc) prop.get(Handler.PATTERN_ATTRIBUTE);
95         if (pattern != null) {
96             setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern)));
97         }
98         String JavaDoc level = (String JavaDoc) prop.get(Handler.LEVEL_ATTRIBUTE);
99         if (level != null && level.length() > 0) {
100             int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf);
101             setThreshold(org.apache.log4j.Level.toLevel(levelVal));
102         }
103         String JavaDoc output = (String JavaDoc) prop.get(Handler.OUTPUT_ATTRIBUTE);
104         output = RelatifEnvironmentPathGetter.getRealPath(output);
105         if (output != null) {
106             super.target = output;
107             if (output.equalsIgnoreCase("System.out")) {
108                 setWriter(new OutputStreamWriter JavaDoc(System.out));
109             } else if (output.equalsIgnoreCase("System.err")) {
110                 setWriter(new OutputStreamWriter JavaDoc(System.err));
111             }
112         }
113         super.activateOptions();
114         return null;
115     }
116
117 }
118
119
Popular Tags