📄 log4jreport.java
字号:
package de.spieleck.app.jacson.report;
import de.spieleck.app.jacson.JacsonConfigException;
import de.spieleck.app.jacson.JacsonReport;
import de.spieleck.app.jacson.JacsonState;
import de.spieleck.app.jacson.util.ConfigUtil;
import de.spieleck.config.ConfigNode;
import de.spieleck.config.ConfigVerify.Acceptor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
* Feed the Jacson output events to log4j and whereever log4j puts it.
* Note this first implementation uses excessive String concatenation
* and is not fast.
* @author fsn
* @author Patrick Carl
*/
public class Log4jReport
implements JacsonReport, Acceptor
{
/** Config: Name of the log4j configuration file. */
public final static String LOG4J_CONFIG = "config";
/** Config: Reread log4j configuration file. */
public final static String LOG4J_REREAD = "reread";
/** Nesting of groups */
LinkedList begins;
/** hash the Logger's */
Map loggers;
/** Keep a stringbuffer for concatenation if sections */
StringBuffer sb;
private JacsonState state;
public Log4jReport() { }
public void init(ConfigNode config)
throws JacsonConfigException
{
begins = new LinkedList();
loggers = new HashMap(20);
sb = new StringBuffer(200);
ConfigUtil.verify(config, this);
String log4jConfig = config.getString(LOG4J_CONFIG, null);
String reread = config.getString(LOG4J_REREAD, null);
if ( log4jConfig == null )
{
BasicConfigurator.configure();
}
else
{
int rr = 1000;
try
{
rr = Integer.parseInt(reread);
}
catch ( Exception e )
{
reread = null;
}
if ( reread != null )
PropertyConfigurator.configureAndWatch(log4jConfig, rr);
else
PropertyConfigurator.configure(log4jConfig);
}
}
public boolean accept(ConfigNode cn)
{
return cn.getName().equals(LOG4J_CONFIG);
}
public void begin(String id)
{
begins.addLast(id);
}
public void report(String localName, String value)
{
Iterator it = begins.iterator();
sb.setLength(0);
while ( it.hasNext() )
sb.append(it.next()).append('.');
sb.append(localName);
String key = sb.toString();
Logger l = (Logger) loggers.get(key);
if ( l == null )
{
l = Logger.getLogger(key);
loggers.put(key, l);
}
l.info(value);
}
public void end()
{
begins.removeLast();
}
public void finish()
{
finish(true);
}
public void finish(boolean close)
{
if ( close )
loggers.clear();
}
public void registerState(JacsonState state)
{
this.state = state;
}
public JacsonState getRegState() {
return state;
}
}
//
// Jacson - Text Filtering with Java.
// Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -