01: /**
02: * Copyright (C) 2001-2003 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * 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: */package org.objectweb.util.monolog.wrapper.log4jMini;
18:
19: import org.apache.log4j.FileAppender;
20: import org.apache.log4j.PatternLayout;
21: import org.objectweb.util.monolog.api.Handler;
22:
23: import java.io.IOException;
24: import java.util.Hashtable;
25: import java.util.Enumeration;
26:
27: /**
28: * This class is the wrapper to the org.apache.log4j.FileAppender
29: *
30: * @author Sebastien Chassande-Barrioz
31: */
32: public class FileHandler extends FileAppender implements Handler {
33:
34: protected byte type = 0;
35: protected Hashtable props = null;
36:
37: /**
38: * It Builds a new MonologFileHandler. It is needed to specify an handler
39: * type.
40: * @param type is the handler type. The possible values are the
41: * HandlerFactory.FILE_HANDLER_TYPE or
42: * HandlerFactory.CONSOLE_HANDLER_TYPE.
43: */
44: public FileHandler(byte type, String name) {
45: super ();
46: this .type = type;
47: setName(name);
48: props = new Hashtable(3);
49: }
50:
51: // IMPLEMENTATION OF THE Handler INTERFACE //
52: //---------------------------------------------//
53:
54: public String getType() {
55: return "file";
56: }
57:
58: public String[] getAttributeNames() {
59: String[] res = new String[props.size()];
60: int i = 0;
61: for (Enumeration en = props.keys(); en.hasMoreElements();) {
62: res[i++] = (String) en.nextElement();
63: }
64: return res;
65: }
66:
67: public Object getAttribute(String key) {
68: return props.get(key);
69: }
70:
71: public Object setAttribute(String key, Object value) {
72: props.put(key, value);
73: if (key.equalsIgnoreCase(Handler.OUTPUT_ATTRIBUTE)) {
74: if ("console".equalsIgnoreCase(type)) {
75: setOption(FILE_OPTION, (String) value);
76: } else if ("file".equalsIgnoreCase(type)) {
77: try {
78: fileName = (String) value;
79: setFile(fileName);
80: } catch (IOException e) {
81: e.printStackTrace();
82: }
83: }
84: } else if (key.equalsIgnoreCase(Handler.PATTERN_ATTRIBUTE)) {
85: setLayout(new PatternLayout(PatternConverter
86: .monolog2log4j((String) value)));
87: } else if (key.equalsIgnoreCase(Handler.APPEND_MODE_ATTRIBUTE)) {
88: setOption(APPEND_OPTION, (String) value);
89: fileAppend = new Boolean((String) value).booleanValue();
90: /*try {
91: setFile(fileName, fileAppend);
92: }
93: catch (IOException e) {
94: e.printStackTrace();
95: } */
96: }
97: return null;
98: }
99: }
|