001: /**
002: * Copyright (C) 2001-2003 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.log4j;
018:
019: import org.apache.log4j.PatternLayout;
020: import org.apache.log4j.RollingFileAppender;
021: import org.objectweb.util.monolog.Monolog;
022: import org.objectweb.util.monolog.api.Handler;
023: import org.objectweb.util.monolog.api.MonologFactory;
024: import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
025:
026: import java.io.IOException;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: /**
031: * This class is the wrapper to the org.apache.log4j.RollingFileAppender
032: *
033: * @author Sebastien Chassande-Barrioz
034: */
035: public class RollingFileHandler extends RollingFileAppender implements
036: Handler {
037:
038: /**
039: * This fields contains the properties of the Handler
040: */
041: protected HashMap prop = null;
042:
043: public RollingFileHandler() {
044: super ();
045: }
046:
047: /**
048: * It Builds a new MonologFileHandler. It is needed to specify an handler
049: * type.
050: * @param name is the handler name.
051: */
052: public RollingFileHandler(String name) {
053: super ();
054: setName(name);
055: prop = new HashMap();
056: }
057:
058: public Map getAttributes() {
059: return prop;
060: }
061:
062: public void setAttributes(Map attributes) {
063: prop.clear();
064: prop.putAll(attributes);
065: Object mf = prop.get("activation");
066: if (mf != null) {
067: prop.remove("activation");
068: setAttribute("activation", mf);
069: }
070: }
071:
072: // IMPLEMENTATION OF THE HandlerConf INTERFACE //
073: //---------------------------------------------//
074:
075: public String getType() {
076: return "rollingfile";
077: }
078:
079: public String[] getAttributeNames() {
080: return (String[]) prop.keySet().toArray(new String[0]);
081: }
082:
083: public Object getAttribute(String key) {
084: return prop.get(key);
085: }
086:
087: public Object setAttribute(String key, Object value) {
088: if (prop == null) {
089: prop = new HashMap();
090: }
091: if (!key.equalsIgnoreCase("activation")) {
092: return prop.put(key, value);
093: } else if (prop.containsKey(key)) {
094: return null; //already activated
095: }
096: MonologFactory mf = (MonologFactory) value;
097: String append = (String) prop
098: .get(Handler.APPEND_MODE_ATTRIBUTE);
099: if (append != null && append.length() > 0) {
100: fileAppend = Boolean.getBoolean(append);
101: } else {
102: fileAppend = true;
103: }
104:
105: String maxSize = (String) prop.get(Handler.MAX_SIZE_ATTRIBUTE);
106: if (maxSize != null && maxSize.length() > 0) {
107: setMaxFileSize(maxSize);
108: }
109: String nbFile = (String) prop
110: .get(Handler.FILE_NUMBER_ATTRIBUTE);
111: if (nbFile != null && nbFile.length() > 0) {
112: setMaxBackupIndex(Integer.valueOf(nbFile).intValue());
113: }
114:
115: String buffersize = (String) prop.get(Handler.BUFFER_ATTRIBUTE);
116: if (buffersize != null && buffersize.length() > 0) {
117: try {
118: setBufferSize(Integer.valueOf(buffersize).intValue());
119: } catch (NumberFormatException e) {
120: Monolog.error(
121: "Bad specified buffer size for the handler '"
122: + name + "': " + buffersize, e);
123: }
124: }
125:
126: String output = (String) prop.get(Handler.OUTPUT_ATTRIBUTE);
127: output = RelatifEnvironmentPathGetter.getRealPath(output);
128: try {
129: setFile(output, fileAppend, bufferedIO, bufferSize);
130: } catch (IOException e) {
131: Monolog.error("Error during the creation of the handler '"
132: + name + "': ", e);
133: }
134:
135: String pattern = (String) prop.get(Handler.PATTERN_ATTRIBUTE);
136: setLayout(new PatternLayout(PatternConverter
137: .monolog2log4j(pattern)));
138:
139: String level = (String) prop.get(Handler.LEVEL_ATTRIBUTE);
140: if (level != null && level.length() > 0) {
141: int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl
142: .evaluate(level, mf);
143: setThreshold(org.apache.log4j.Level.toLevel(levelVal));
144: }
145: super.activateOptions();
146: return null;
147: }
148: }
|