KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.log4j.PatternLayout;
22 import org.apache.log4j.RollingFileAppender;
23 import org.objectweb.util.monolog.Monolog;
24 import org.objectweb.util.monolog.api.Handler;
25 import org.objectweb.util.monolog.api.MonologFactory;
26 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
27
28 import java.io.IOException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * This class is the wrapper to the org.apache.log4j.RollingFileAppender
34  *
35  * @author Sebastien Chassande-Barrioz
36  */

37 public class RollingFileHandler
38     extends RollingFileAppender
39     implements Handler {
40
41     /**
42      * This fields contains the properties of the Handler
43      */

44     protected HashMap JavaDoc prop = null;
45
46     public RollingFileHandler() {
47         super();
48     }
49
50     /**
51      * It Builds a new MonologFileHandler. It is needed to specify an handler
52      * type.
53      * @param name is the handler name.
54      */

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

78     public String JavaDoc getType() {
79         return "rollingfile";
80     }
81
82     public String JavaDoc[] getAttributeNames() {
83         return (String JavaDoc[]) prop.keySet().toArray(new String JavaDoc[0]);
84     }
85
86     public Object JavaDoc getAttribute(String JavaDoc key) {
87         return prop.get(key);
88     }
89
90     public Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value) {
91         if (prop == null) {
92             prop = new HashMap JavaDoc();
93         }
94         if (!key.equalsIgnoreCase("activation")) {
95             return prop.put(key, value);
96         } else if (prop.containsKey(key)) {
97             return null; //already activated
98
}
99         MonologFactory mf = (MonologFactory) value;
100         String JavaDoc append = (String JavaDoc) prop.get(Handler.APPEND_MODE_ATTRIBUTE);
101         if (append != null && append.length() > 0) {
102             fileAppend = Boolean.getBoolean(append);
103         } else {
104             fileAppend = true;
105         }
106         
107         String JavaDoc maxSize = (String JavaDoc) prop.get(Handler.MAX_SIZE_ATTRIBUTE);
108         if (maxSize != null && maxSize.length() > 0) {
109             setMaxFileSize(maxSize);
110         }
111         String JavaDoc nbFile = (String JavaDoc) prop.get(Handler.FILE_NUMBER_ATTRIBUTE);
112         if (nbFile != null && nbFile.length() > 0) {
113             setMaxBackupIndex(Integer.valueOf(nbFile).intValue());
114         }
115
116         String JavaDoc buffersize = (String JavaDoc) prop.get(Handler.BUFFER_ATTRIBUTE);
117         if (buffersize != null && buffersize.length() > 0) {
118             try {
119                 setBufferSize(Integer.valueOf(buffersize).intValue());
120             } catch (NumberFormatException JavaDoc e) {
121                 Monolog.error("Bad specified buffer size for the handler '"
122                         + name + "': " + buffersize, e);
123             }
124         }
125
126         String JavaDoc output = (String JavaDoc) prop.get(Handler.OUTPUT_ATTRIBUTE);
127         output = RelatifEnvironmentPathGetter.getRealPath(output);
128         try {
129             setFile(output, fileAppend, bufferedIO, bufferSize);
130         } catch (IOException JavaDoc e) {
131             Monolog.error("Error during the creation of the handler '"
132                     + name + "': ", e);
133         }
134         
135         String JavaDoc pattern = (String JavaDoc) prop.get(Handler.PATTERN_ATTRIBUTE);
136         setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern)));
137
138         String JavaDoc level = (String JavaDoc) prop.get(Handler.LEVEL_ATTRIBUTE);
139         if (level != null && level.length() > 0) {
140             int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf);
141             setThreshold(org.apache.log4j.Level.toLevel(levelVal));
142         }
143         super.activateOptions();
144         return null;
145     }
146 }
147
Popular Tags