at.spardat.xma.boot.logger.LogManager Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* Created on : 25.06.2003
* Created by : s3595
*/
package at.spardat.xma.boot.logger;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
import at.spardat.xma.boot.Statics;
/**
* LogManager
*
* @author s3595 (CGS)
* @version $Id: LogManager.java 5887 2010-06-07 09:58:14Z gub $
*
*/
public class LogManager {
/* The global LogManager object */
private static LogManager manager;
/* default and fallback loglevel */
private final static LogLevel defaultLevel = LogLevel.WARNING;
/* current active log level */
private LogLevel currentLogLevel;
/* Table of known loggers. Maps names to Loggers. */
private Hashtable loggers = new Hashtable();
/* configuration properties */
private Properties props;
/* default log handler */
private ILogHandler defaultHandler = new LogHandlerDefault();
/* log handlers */
private ArrayList handlers = new ArrayList(3);
static {
if (manager == null) {
manager = new LogManager();
}
}
/**
* It is intended that there only be one LogManager object, whose value is
* retrieved by calling Logmanager.getLogManager.
*/
protected LogManager() {
currentLogLevel = defaultLevel;
handlers.add(defaultHandler);
}
/** Return the global LogManager object. */
public static LogManager getLogManager() {
return manager;
}
/**
* Reinitialize the logging properties and reread the logging configuration.
*/
public void setConfiguration( Properties in ) {
props = in;
String strDebug = (String)props.get( Statics.CFG_PROP_LOGLEVEL ); //$NON-NLS-1$
LogLevel newlevel = LogLevel.getLogLevelNamed(strDebug);
if(newlevel != null){
currentLogLevel = newlevel;
}
String strHandlers = (String)props.get( Statics.CFG_PROP_LOGLHANDLERS ); //$NON-NLS-1$
if(strHandlers!=null) {
handlers = new ArrayList();
loadHandlers( strHandlers );
if( handlers.size()== 0 ) {
handlers.add(defaultHandler);
}
}
notifyLoggers(currentLogLevel, true );
}
public void notifyLoggers( LogLevel newlevel, boolean notifyHandlers ) {
for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();) {
Logger element = (Logger)loggers.get((String)iter.next());
element.setLevel(newlevel);
element.setHandlers(handlers);
}
}
public synchronized boolean addLogger(Logger logger) {
String name = logger.getName();
if (name == null) {
throw new NullPointerException();
}
Logger old = (Logger) loggers.get(name);
if (old != null) {
// We already have a registered logger with the given name.
return false;
}
loggers.put(name, logger);
logger.setLevel( currentLogLevel );
logger.setHandlers( handlers );
return true;
}
/**
* Method to find a named logger.
*
* @param name name of the logger
* @return matching logger or null if none is found
*/
public synchronized Logger getLogger(String name) {
return (Logger) loggers.get(name);
}
private synchronized void loadHandlers(String strHandlers) {
if(strHandlers==null)
return;
String names[] = parseClassNames(strHandlers);
for (int i = 0; i < names.length; i++) {
String word = names[i];
try {
Class clz = this.getClass().getClassLoader().loadClass(word);
ILogHandler h = (ILogHandler) clz.newInstance();
h.setProperties(props);
handlers.add(h);
} catch (Exception ex) {
System.err.println("Can't load log handler \"" + word + "\"");
System.err.println("" + ex);
}
}
return;
}
// get a list of whitespace separated classnames from a property.
private String[] parseClassNames(String handlers) {
if (handlers == null) {
return new String[0];
}
handlers = handlers.trim();
int ix = 0;
Vector result = new Vector();
while (ix < handlers.length()) {
int end = ix;
while (end < handlers.length()) {
if (Character.isWhitespace(handlers.charAt(end))) {
break;
}
if (handlers.charAt(end) == ',') {
break;
}
end++;
}
String word = handlers.substring(ix, end);
ix = end+1;
word = word.trim();
if (word.length() == 0) {
continue;
}
result.add(word);
}
return (String[]) result.toArray(new String[result.size()]);
}
/**
* Get the fully quallified name of the used logfile.
* If no logfile is used, this method returns null
.
* @since 1.6.0
*/
public String getLogFileName() {
for(Iterator it=handlers.iterator();it.hasNext();) {
Object handler = it.next();
if(handler instanceof LogHandlerFile) {
return ((LogHandlerFile)handler).getLogFileName();
}
}
return null;
}
}