All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.boot.logger.Logger 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.Iterator;

/**
 * Logger
 *
 * @author s3595 Chr. Schaefer (CGS)
 * @version $Id: Logger.java 2084 2007-11-27 14:53:31Z s3460 $
 *
 */
public class Logger implements ILogger {

    /* logger name */
    private String name;

    /* value of logging = off */
    private static final int offValue = LogLevel.OFF.intValue();

    /* log level */
    private volatile int levelValue;
    private LogLevel levelObject;

    /* default log handler */
    private ILogHandler defaultHandler = new LogHandlerDefault();

    /* log handlers */
    private ArrayList handlers = new ArrayList(1);


    public static synchronized Logger getLogger(String name) {
        LogManager manager = LogManager.getLogManager();
        Logger result = manager.getLogger(name);
        if (result == null) {
            result = new Logger( name );
            manager.addLogger(result);
            result = manager.getLogger(name);
        }
      return result;
    }

    public Logger( String strNameIn ) {
       name = strNameIn;
       handlers.add(defaultHandler);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#getLevel()
     */
    public LogLevel getLevel() {
        return levelObject;
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#logrb(at.spardat.xma.boot.logger.LogLevel, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Throwable)
     */
    public void logrb(
        LogLevel level,
        String sourceClass,
        String sourceMethod,
        String msg,
        Throwable thrown) {

            if (level.intValue() < levelValue || levelValue == offValue) {
                return;
            }
            LogRecord lr = new LogRecord(level, msg);
            lr.setSourceClassName(sourceClass);
            lr.setSourceMethodName(sourceMethod);
            lr.setThrown(thrown);

        this.doLog(lr);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#setLevel(at.spardat.xma.boot.logger.LogLevel)
     */
    public void setLevel(LogLevel newLevel) throws SecurityException {
        this.levelObject = newLevel;
        this.levelValue  = levelObject.intValue();
    }

    private void doLog(LogRecord lr) {
        if(lr==null) return;

        lr.setLoggerName(name);
        Iterator iter = handlers.iterator();
        while (iter.hasNext()) {
            ILogHandler element = (ILogHandler)iter.next();
            element.publish(lr);
        }
    }


    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#log(at.spardat.xma.boot.logger.LogLevel, java.lang.String)
     */
    public void log(LogLevel level, String msg) {
        if (level.intValue() < levelValue || levelValue == offValue) {
            return;
        }
        LogRecord lr = new LogRecord(level, msg);
        doLog(lr);
    }


    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#log(at.spardat.xma.boot.logger.LogLevel, java.lang.String, java.lang.Object)
     */
    public void log(LogLevel level, String msg, Object param1) {
        if (level.intValue() < levelValue || levelValue == offValue) {
            return;
        }

        LogRecord lr = new LogRecord(level, msg);
        Object params[] = { param1 };
        lr.setParameters(params);
        doLog(lr);

    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#log(at.spardat.xma.boot.logger.LogLevel, java.lang.String, java.lang.Object[])
     */
    public void log(LogLevel level, String msg, Object[] params) {
        if (level.intValue() < levelValue || levelValue == offValue) {
            return;
        }
        LogRecord lr = new LogRecord(level, msg);
        lr.setParameters(params);
        doLog(lr);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#log(at.spardat.xma.boot.logger.LogLevel, java.lang.String, java.lang.Throwable)
     */
    public void log(LogLevel level, String msg, Throwable thrown) {
        if (level.intValue() < levelValue || levelValue == offValue) {
            return;
        }
        LogRecord lr = new LogRecord(level, msg);
        lr.setThrown(thrown);
        doLog(lr);
    }


    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#info(java.lang.String)
     */
    public void info(String msg) {
        if (LogLevel.INFO.intValue() < levelValue || levelValue == offValue) {
            return;
        }
        LogRecord lr = new LogRecord(LogLevel.INFO, msg);
        doLog(lr);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#severe(java.lang.String)
     */
    public void severe(String msg) {
        if (LogLevel.SEVERE.intValue() < levelValue || levelValue == offValue) {
            return;
        }
        LogRecord lr = new LogRecord(LogLevel.SEVERE, msg);
        doLog(lr);
    }

    /* (non-Javadoc)
     * @see at.spardat.xma.boot.logger.ILogger#warning(java.lang.String)
     */
    public void warning(String msg) {
        if (LogLevel.WARNING.intValue() < levelValue || levelValue == offValue) {
            return;
        }
        LogRecord lr = new LogRecord(LogLevel.WARNING, msg);
        doLog(lr);
    }

    /**
     * @return String name
     */
    public String getName() {
        return name;
    }

    /**
     * @param string
     */
    public void setName(String string) {
        name = string;
    }

    /**
     * @return array of handlers
     */
    public ArrayList getHandlers() {
        return handlers;
    }

    /**
     * @param list
     */
    public void setHandlers(ArrayList list) {
        handlers = list;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy