001: //ConsoleOutErrHandler.java
002: //-------------------------------------
003: //part of YACY
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2004
007: //
008: //This file ist contributed by Martin Thelian
009: //last major change: $LastChangedDate: 2008-01-23 23:08:32 +0000 (Mi, 23 Jan 2008) $ by $LastChangedBy: orbiter $
010: //Revision: $LastChangedRevision: 4382 $
011: //
012: //This program is free software; you can redistribute it and/or modify
013: //it under the terms of the GNU General Public License as published by
014: //the Free Software Foundation; either version 2 of the License, or
015: //(at your option) any later version.
016: //
017: //This program is distributed in the hope that it will be useful,
018: //but WITHOUT ANY WARRANTY; without even the implied warranty of
019: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: //GNU General Public License for more details.
021: //
022: //You should have received a copy of the GNU General Public License
023: //along with this program; if not, write to the Free Software
024: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: //Using this software in any meaning (reading, learning, copying, compiling,
027: //running) means that you agree that the Author(s) is (are) not responsible
028: //for cost, loss of data or any harm that may be caused directly or indirectly
029: //by usage of this softare or this documentation. The usage of this software
030: //is on your own risk. The installation and usage (starting/running) of this
031: //software may allow other people or application to access your computer and
032: //any attached devices and is highly dependent on the configuration of the
033: //software which must be done by the user of the software; the author(s) is
034: //(are) also not responsible for proper configuration and usage of the
035: //software, even if provoked by documentation provided together with
036: //the software.
037: //
038: //Any changes to this file according to the GPL as documented in the file
039: //gpl.txt aside this file in the shipment you received can be done to the
040: //lines that follows this copyright notice here, but changes must not be
041: //done inside the copyright notive above. A re-distribution must contain
042: //the intact and unchanged copyright notice.
043: //Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.server.logging;
046:
047: import java.io.UnsupportedEncodingException;
048: import java.util.logging.ConsoleHandler;
049: import java.util.logging.Filter;
050: import java.util.logging.Formatter;
051: import java.util.logging.Handler;
052: import java.util.logging.Level;
053: import java.util.logging.LogManager;
054: import java.util.logging.LogRecord;
055: import java.util.logging.SimpleFormatter;
056:
057: public final class ConsoleOutErrHandler extends Handler {
058:
059: private boolean ignoreCtrlChr = false;
060: private Level splitLevel = Level.WARNING;
061: private final Handler stdOutHandler = new ConsoleOutHandler();
062: private final Handler stdErrHandler = new ConsoleHandler();
063:
064: public ConsoleOutErrHandler() {
065: this .stdOutHandler.setLevel(Level.FINEST);
066: this .stdErrHandler.setLevel(Level.WARNING);
067: configure();
068: }
069:
070: /**
071: * Get any configuration properties set
072: */
073: private void configure() {
074: LogManager manager = LogManager.getLogManager();
075: String className = getClass().getName();
076:
077: String level = manager.getProperty(className + ".level");
078: setLevel((level == null) ? Level.INFO : Level.parse(level));
079:
080: Level levelStdOut = parseLevel(manager.getProperty(className
081: + ".levelStdOut"));
082: Level levelSplit = parseLevel(manager.getProperty(className
083: + ".levelSplit"));
084: Level levelStdErr = parseLevel(manager.getProperty(className
085: + ".levelStdErr"));
086: setLevels(levelStdOut, levelSplit, levelStdErr);
087:
088: String filter = manager.getProperty(className + ".filter");
089: setFilter(makeFilter(filter));
090:
091: String formatter = manager
092: .getProperty(className + ".formatter");
093: setFormatter(makeFormatter(formatter));
094:
095: String encoding = manager.getProperty(className + ".encoding");
096: try {
097: this .stdOutHandler.setEncoding(encoding);
098: this .stdErrHandler.setEncoding(encoding);
099: } catch (UnsupportedEncodingException e) {
100: e.printStackTrace();
101: }
102:
103: String ignoreCtrlChrStr = manager.getProperty(className
104: + ".ignoreCtrlChr");
105: this .ignoreCtrlChr = (ignoreCtrlChrStr == null) ? false
106: : ignoreCtrlChrStr.equalsIgnoreCase("true");
107:
108: }
109:
110: private Level parseLevel(String levelName) {
111: try {
112: return (levelName == null) ? Level.INFO : Level
113: .parse(levelName);
114: } catch (Exception e) {
115: return Level.ALL;
116: }
117: }
118:
119: private Filter makeFilter(String name) {
120: if (name == null)
121: return null;
122:
123: Filter f = null;
124: try {
125: Class<?> c = Class.forName(name);
126: f = (Filter) c.newInstance();
127: } catch (Exception e) {
128: if (name != null) {
129: System.err.println("Unable to load filter: " + name);
130: }
131: }
132: return f;
133: }
134:
135: private Formatter makeFormatter(String name) {
136: if (name == null)
137: return null;
138:
139: Formatter f = null;
140: try {
141: Class<?> c = Class.forName(name);
142: f = (Formatter) c.newInstance();
143: } catch (Exception e) {
144: f = new SimpleFormatter();
145: }
146: return f;
147: }
148:
149: public void publish(LogRecord record) {
150: if (!isLoggable(record))
151: return;
152:
153: if (this .ignoreCtrlChr) {
154: String msg = record.getMessage();
155: if (msg != null) {
156: msg = msg
157: .replaceAll(
158: "[\u0000-\u0008\u000B\u000C\u000E-\u001F]",
159: " ");
160: }
161: record.setMessage(msg);
162: }
163:
164: if (record.getLevel().intValue() >= splitLevel.intValue()) {
165: this .stdErrHandler.publish(record);
166: } else {
167: this .stdOutHandler.publish(record);
168: }
169: }
170:
171: public void flush() {
172: this .stdOutHandler.flush();
173: this .stdErrHandler.flush();
174: }
175:
176: public void close() throws SecurityException {
177: this .stdOutHandler.close();
178: this .stdErrHandler.close();
179: }
180:
181: public synchronized void setLevel(Level newLevel)
182: throws SecurityException {
183: super .setLevel(newLevel);
184: }
185:
186: public void setLevels(Level stdOutLevel, Level splitLevel,
187: Level stdErrLevel) throws SecurityException {
188: this .stdOutHandler.setLevel(stdOutLevel);
189: this .splitLevel = splitLevel;
190: this .stdErrHandler.setLevel(stdErrLevel);
191: }
192:
193: public void setFormatter(Formatter newFormatter)
194: throws SecurityException {
195: super .setFormatter(newFormatter);
196: if (newFormatter == null)
197: return;
198: try {
199: this .stdOutHandler.setFormatter((Formatter) newFormatter
200: .getClass().newInstance());
201: this .stdErrHandler.setFormatter((Formatter) newFormatter
202: .getClass().newInstance());
203: } catch (Exception e) {
204: throw new SecurityException(e.getMessage());
205: }
206: }
207:
208: public void setFilter(Filter newFilter) throws SecurityException {
209: super .setFilter(newFilter);
210: if (newFilter == null)
211: return;
212: try {
213: this .stdOutHandler.setFilter((Filter) newFilter.getClass()
214: .newInstance());
215: this .stdErrHandler.setFilter((Filter) newFilter.getClass()
216: .newInstance());
217: } catch (Exception e) {
218: throw new SecurityException(e.getMessage());
219: }
220: }
221: }
|