KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4jMini > FileHandler


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.log4jMini;
20
21 import org.apache.log4j.FileAppender;
22 import org.apache.log4j.PatternLayout;
23 import org.objectweb.util.monolog.api.Handler;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 /**
30  * This class is the wrapper to the org.apache.log4j.FileAppender
31  *
32  * @author Sebastien Chassande-Barrioz
33  */

34 public class FileHandler extends FileAppender implements Handler {
35
36     protected byte type = 0;
37     protected Hashtable JavaDoc props = null;
38
39     /**
40      * It Builds a new MonologFileHandler. It is needed to specify an handler
41      * type.
42      * @param type is the handler type. The possible values are the
43      * HandlerFactory.FILE_HANDLER_TYPE or
44      * HandlerFactory.CONSOLE_HANDLER_TYPE.
45      */

46     public FileHandler(byte type, String JavaDoc name) {
47         super();
48         this.type = type;
49         setName(name);
50         props = new Hashtable JavaDoc(3);
51     }
52
53     // IMPLEMENTATION OF THE Handler INTERFACE //
54
//---------------------------------------------//
55

56     public String JavaDoc getType() {
57         return "file";
58     }
59
60     public String JavaDoc[] getAttributeNames() {
61         String JavaDoc[] res = new String JavaDoc[props.size()];
62         int i=0;
63         for(Enumeration JavaDoc en=props.keys(); en.hasMoreElements();) {
64             res[i++] = (String JavaDoc) en.nextElement();
65         }
66         return res;
67     }
68
69     public Object JavaDoc getAttribute(String JavaDoc key) {
70         return props.get(key);
71     }
72
73     public Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value) {
74         props.put(key, value);
75         if (key.equalsIgnoreCase(Handler.OUTPUT_ATTRIBUTE)) {
76             if ("console".equalsIgnoreCase(type)) {
77                 setOption(FILE_OPTION, (String JavaDoc) value);
78             } else if ("file".equalsIgnoreCase(type)) {
79                 try {
80                     fileName=(String JavaDoc) value;
81                     setFile(fileName);
82                 } catch (IOException JavaDoc e) {
83                     e.printStackTrace();
84                 }
85             }
86         }
87         else if (key.equalsIgnoreCase(Handler.PATTERN_ATTRIBUTE)) {
88             setLayout(
89                 new PatternLayout(
90                     PatternConverter.monolog2log4j((String JavaDoc) value)));
91         }
92         else if (key.equalsIgnoreCase(Handler.APPEND_MODE_ATTRIBUTE)) {
93             setOption(APPEND_OPTION, (String JavaDoc) value);
94             fileAppend = new Boolean JavaDoc( (String JavaDoc) value).booleanValue();
95             /*try {
96                 setFile(fileName, fileAppend);
97             }
98             catch (IOException e) {
99                   e.printStackTrace();
100             } */

101         }
102         return null;
103     }
104 }
105
Popular Tags