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.objectweb.util.monolog.api.Handler;
020: import org.objectweb.util.monolog.api.MonologFactory;
021: import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
022: import org.apache.log4j.PatternLayout;
023: import org.apache.log4j.ConsoleAppender;
024:
025: import java.util.HashMap;
026: import java.util.Map;
027: import java.io.OutputStreamWriter;
028:
029: /**
030: *
031: * @author Sebastien Chassande-Barrioz
032: */
033: public class ConsoleHandler extends ConsoleAppender implements Handler {
034:
035: /**
036: * This fields contains the properties of the Handler
037: */
038: protected HashMap prop = null;
039:
040: public ConsoleHandler() {
041: super ();
042: }
043:
044: /**
045: * It Builds a new ConsoleHandler.
046: * @param name is the handler name.
047: */
048: public ConsoleHandler(String name) {
049: super ();
050: setName(name);
051: prop = new HashMap();
052: }
053:
054: public Map getAttributes() {
055: return prop;
056: }
057:
058: public void setAttributes(Map attributes) {
059: prop.clear();
060: prop.putAll(attributes);
061: Object mf = prop.get("activation");
062: if (mf != null) {
063: prop.remove("activation");
064: setAttribute("activation", mf);
065: }
066: }
067:
068: // IMPLEMENTATION OF THE Handler INTERFACE //
069: //---------------------------------------------//
070:
071: public String getType() {
072: return "console";
073: }
074:
075: public String[] getAttributeNames() {
076: return (String[]) prop.keySet().toArray(new String[0]);
077: }
078:
079: public Object getAttribute(String key) {
080: return prop.get(key);
081: }
082:
083: public Object setAttribute(String key, Object value) {
084: if (prop == null)
085: prop = new HashMap();
086: if (!key.equalsIgnoreCase("activation")) {
087: return prop.put(key, value);
088: } else if (prop.containsKey(key)) {
089: return null; //already activated
090: }
091: MonologFactory mf = (MonologFactory) value;
092: String pattern = (String) prop.get(Handler.PATTERN_ATTRIBUTE);
093: if (pattern != null) {
094: setLayout(new PatternLayout(PatternConverter
095: .monolog2log4j(pattern)));
096: }
097: String level = (String) prop.get(Handler.LEVEL_ATTRIBUTE);
098: if (level != null && level.length() > 0) {
099: int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl
100: .evaluate(level, mf);
101: setThreshold(org.apache.log4j.Level.toLevel(levelVal));
102: }
103: String output = (String) prop.get(Handler.OUTPUT_ATTRIBUTE);
104: output = RelatifEnvironmentPathGetter.getRealPath(output);
105: if (output != null) {
106: super .target = output;
107: if (output.equalsIgnoreCase("System.out")) {
108: setWriter(new OutputStreamWriter(System.out));
109: } else if (output.equalsIgnoreCase("System.err")) {
110: setWriter(new OutputStreamWriter(System.err));
111: }
112: }
113: super.activateOptions();
114: return null;
115: }
116:
117: }
|