Package org.jzonic.jlo

Source Code of org.jzonic.jlo.LogManager

package org.jzonic.jlo;

import org.jzonic.jlo.error.ErrorHandler;
import org.jzonic.jlo.events.FileListener;
import org.jzonic.jlo.events.FileListenerEvent;
import org.jzonic.jlo.formatter.DefaultFormatter;
import org.jzonic.jlo.formatter.Formatter;
import org.jzonic.jlo.handler.ConsoleHandler;
import org.jzonic.jlo.handler.Handler;
import org.jzonic.jlo.reader.*;
import org.jzonic.jlo.processor.*;

import java.util.HashMap;
/**
* This class manages all LogConfigurations. It is the point of entry for jLo.
* There is only one instance of the LogManager to make sure that all classes
* in one JVM will work with the same configurations. <BR/>
* In order to get a Logger you have to call:
* <pre>
* private static final Logger logger = LogManager.getLogger("com.foo.app");
* </pre>
* In this case the LogManager when instanciated will search for a file
* called "jlo_logging.xml" in your classpath and process it.<BR/>
* This method is error save that means that it will always return a valid
* Logger. <BR/>
* Since jLo supports many LogConfigurations you can also use this method:
* <pre>
* private static final Logger logger = LogManager.getLogger("com.foo.app","foo");
* </pre>
* If you provide a configuration name then the LogManager will search for a
* file called "foo_logging.xml" in your classpath and process it.
*
*
*@author     Andreas Mecky
*@author     Terry Dye
*@created    29. Januar 2002
*@version    1.0
*/
public class LogManager implements FileListener {
   
    private LogConfiguration defaultConfiguration;
    private static LogManager lm = null;
    private HashMap configurations;
    private Logger defaultLogger;
    private Channel defaultChannel;
    private FileWatcher fileWatcher;
   
    private LogManager() {
        configurations = new HashMap();
        XMLFileReader reader = new XMLFileReader();
        reader.setFileName("jlo_logging.xml");
        readConfiguration(reader,"Default");
        fileWatcher = new FileWatcher();
        fileWatcher.addFileListener(this, "Default");
        defaultConfiguration = new LogConfiguration("default");
        defaultConfiguration.addLogger(getDefaultLogger());
        defaultLogger = getDefaultLogger();
        addDefaultChannel();
    }
   
    /**
     * This method returns the one and only instance of the LogManager.
     *
     * @return the instance of the LogManager
     */
    public static LogManager getInstance() {
        if ( lm == null ) {
            lm = new LogManager();
        }
        return lm;
    }
   
    /**
     * This method returns a Logger from the LogConfiguration.
     * The name of the LogConfiguration is default and the corresponding
     * file is jlo_logging.xml. This method uses the LogConfiguration.getLogger
     * method.
     *
     * @param loggerName the name of the logger
     * @return the Logger or the DefaultLogger if not found
     */
    public static Logger getLogger(String loggerName) {
        return getLogger(loggerName,"Default");
    }
   
    /**
     * This methods returns a logger for the given name from the specific
     * logconfiguration. If a logger with the given name does not exist then
     * the LogConfiguration will try to search for it by going up the hierarchy.
     * If still no logger is found then this method will return the DefaultLogger.
     * This method will never return null therefore it is save to do this:
     * <pre>
     * private static final Logger logger = LogManager.getLogger("org.foo.app");
     * </pre>
     * in your class. If there is no instance of the LogManager so far then
     * a new one will be created.
     *
     * @param loggerName the name of the logger
     * @param configurationName the name of the configuration
     * @return the requested logger or the next one in the hierarchy or the default
     */
    public static Logger getLogger(String loggerName,String configurationName) {
        if ( lm == null ) {
            lm = new LogManager();
        }
        if ( configurationName == null ) {
            Logger logger = lm.defaultConfiguration.getLogger(loggerName);
            if ( logger == null ) {
                ErrorHandler.reportError("Logger "+loggerName+" in "+configurationName+" not found. Using default logger");
                return lm.defaultLogger;
            }
            return logger;
        }
        else {
            Logger logger = lm.getLogConfiguration(configurationName).getLogger(loggerName);
            if ( logger == null ) {
                ErrorHandler.reportError("Logger "+loggerName+" in "+configurationName+" not found. Using default logger");
                return lm.defaultLogger;
            }
            return logger;
        }
    }
   
    /**
     * @param channelName
     * @return
     */
    public static Channel getChannel(String channelName) {
        return getChannel(channelName,"Default");
    }
   
    /**
     * @param channelName
     * @param configurationName
     * @return
     */
    public static Channel getChannel(String channelName,String configurationName) {
        if ( lm == null ) {
            lm = new LogManager();
        }
        if ( configurationName == null ) {
            Channel channel = lm.getLogConfiguration(configurationName).getChannel(channelName);
            if ( channel == null ) {
                return lm.defaultChannel;
            }
            return channel;
        }
        else {
            Channel channel = lm.getLogConfiguration(configurationName).getChannel(channelName);
            if ( channel == null ) {
                return lm.defaultChannel;
            }
            return channel;
        }
    }
   
    /**
     * @param channelName
     * @return
     */
    public static boolean isChannelOn(String channelName) {
        return isChannelOn(channelName,"Default");
    }
   
    /**
     * @param channelName
     * @param configurationName
     * @return
     */
    public static boolean isChannelOn(String channelName,String configurationName) {
        Channel channel = getChannel(channelName, configurationName);
        if ( channel == null ) {
            return false;
        }
        return channel.isOn();
    }
   
    public LogConfiguration getLogConfiguration() {
        return getLogConfiguration("Default");
    }
   
    public LogConfiguration getLogConfiguration(String configurationName) {
        if ( configurations.containsKey(configurationName) ) {           
            return (LogConfiguration)configurations.get(configurationName);
        }
        else {           
            XMLFileReader reader = new XMLFileReader();
            reader.setFileName(configurationName+"_logging.xml");
            try {
                LogConfiguration lc = reader.parseConfiguration(configurationName);
                configurations.put(lc.getName(),lc);
                fileWatcher.addFileListener(this,configurationName);
                return lc;
            }
            catch (ReaderException re) {
                ErrorHandler.reportError("Could not find file:"+configurationName+"_logging.xml in the classpath");
                return defaultConfiguration;
            }
        }
    }
   
    /**
     *
     */
    //TODO: do some error checking
    //TODO: set up a filewatcher if it is not already running
    public boolean readConfiguration(LogConfigurationReader reader,String configurationName) {
        try {
            LogConfiguration lc = reader.parseConfiguration(configurationName);
            if ( configurationName == null ) {
                defaultConfiguration = lc;
            }
            else {
                configurations.put(configurationName, lc);
            }
            return true;
        }
        catch (ReaderException re) {
            return false;
        }
    }
   
    /**
     */
    private Logger getDefaultLogger() {
        LogGenerator lg = getDefaultLogGenerator();
        Logger logger = new Logger("Default", Target.all.intValue(),"Default");
        logger.addLogGenerator(lg);
        return logger;
    }
   
    private void addDefaultChannel() {
        LogGenerator lg = getDefaultLogGenerator();
        Channel channel = new Channel("Default",lg,false);
        defaultChannel = channel;
    }
   
    private LogGenerator getDefaultLogGenerator() {
        Handler consoleHandler = new ConsoleHandler("Default");
        Formatter defaultFormatter = new DefaultFormatter("Default");
        LogGenerator lg = new LogGenerator("Default", consoleHandler, defaultFormatter);
        return lg;
    }
   
    /** This method, once implemented, will be called when the File object
     * itself changes.
     *
     * @param FileListenerEvent The FileListener object.
     *
     */
    public void fileChanged(FileListenerEvent e) {
        String configName = e.getConfigurationName();       
        reloadLogConfiguration(configName);
    }
   
    private void reloadLogConfiguration(String configurationName) {               
        XMLFileReader reader = new XMLFileReader();
        reader.setFileName(configurationName+"_logging.xml");
        try {
            LogConfiguration lc = reader.parseConfiguration(configurationName);           
            configurations.put(lc.getName(),lc);           
        }
        catch (ReaderException re) {           
        }       
    }
   
    /**
     * Call thi smethod if you want to force the LogProcessor to write
     * all pending log statements immediately
     */
    public void flush() {
        LogProcessorFactory.getLogProcessor().flush();
    }
}
TOP

Related Classes of org.jzonic.jlo.LogManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.