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.FileAppender;
020: import org.apache.log4j.PatternLayout;
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.FileAppender
032: *
033: * @author Sebastien Chassande-Barrioz
034: */
035: public class FileHandler extends FileAppender implements Handler {
036:
037: /**
038: * This fields contains the properties of the Handler
039: */
040: protected HashMap prop = null;
041:
042: public FileHandler() {
043: super ();
044: }
045:
046: /**
047: * It Builds a new FileHandler.
048: * @param name is the handler name.
049: */
050: public FileHandler(String name) {
051: super ();
052: setName(name);
053: prop = new HashMap();
054: }
055:
056: public Map getAttributes() {
057: return prop;
058: }
059:
060: public void setAttributes(Map attributes) {
061: prop.clear();
062: prop.putAll(attributes);
063: Object mf = prop.get("activation");
064: if (mf != null) {
065: prop.remove("activation");
066: setAttribute("activation", mf);
067: }
068: }
069:
070: // IMPLEMENTATION OF THE Handler INTERFACE //
071: //---------------------------------------------//
072:
073: public String getType() {
074: return "file";
075: }
076:
077: public String[] getAttributeNames() {
078: return (String[]) prop.keySet().toArray(new String[0]);
079: }
080:
081: public Object getAttribute(String key) {
082: return prop.get(key);
083: }
084:
085: public Object setAttribute(String key, Object value) {
086: if (prop == null) {
087: prop = new HashMap();
088: }
089: if (!key.equalsIgnoreCase("activation")) {
090: return prop.put(key, value);
091: } else if (prop.containsKey(key)) {
092: return null; //already activated
093: }
094: MonologFactory mf = (MonologFactory) value;
095: String output = (String) prop.get(Handler.OUTPUT_ATTRIBUTE);
096: output = RelatifEnvironmentPathGetter.getRealPath(output);
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 buffersize = (String) prop.get(Handler.BUFFER_ATTRIBUTE);
106: if (buffersize != null && buffersize.length() > 0) {
107: try {
108: setBufferSize(Integer.valueOf(buffersize).intValue());
109: } catch (NumberFormatException e) {
110: Monolog.error(
111: "Bad specified buffer size for the handler '"
112: + name + "': " + buffersize, e);
113: }
114: }
115:
116: try {
117: setFile(output, fileAppend, bufferedIO, bufferSize);
118: } catch (IOException e) {
119: Monolog.error("Error during the creation of the handler '"
120: + name + "': ", e);
121: }
122:
123: String pattern = (String) prop.get(Handler.PATTERN_ATTRIBUTE);
124: setLayout(new PatternLayout(PatternConverter
125: .monolog2log4j(pattern)));
126:
127: String level = (String) prop.get(Handler.LEVEL_ATTRIBUTE);
128: if (level != null && level.length() > 0) {
129: int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl
130: .evaluate(level, mf);
131: setThreshold(org.apache.log4j.Level.toLevel(levelVal));
132: }
133:
134: super.activateOptions();
135: return null;
136: }
137:
138: }
|