Logger.java :  » Development » jLo » org » jzonic » jlo » Java Open Source

Java Open Source » Development » jLo 
jLo » org » jzonic » jlo » Logger.java

package org.jzonic.jlo;

import org.apache.commons.logging.Log;
import org.jzonic.jlo.filter.LogFilter;
import org.jzonic.jlo.formatter.CallerStack;
import org.jzonic.jlo.processor.LogProcessor;
import org.jzonic.jlo.processor.LogProcessorFactory;

import java.util.Vector;
/** 
 */
public class Logger implements Log {
        
    private static final LogProcessor processor = LogProcessorFactory.getLogProcessor();
    private Vector generators;
    private int targets;
    private LogFilter filter = null;
    private String sourceClass = null;
    private String sourceMethod = null;    
    private String name;
    private String configName;        
    /** 
     *  Constructor for the Logger object
     */
    private Logger() {        
    }
    
    public Logger(String name,int targets,String configName) {
        this.name = name;        
        this.targets = targets;        
        generators = new Vector();
        this.configName = configName;
    }
    
    public void addLogGenerator(LogGenerator lg) {
        generators.add(lg);
    }
    
    public Vector getLogGenerators() {
        return generators;
    }
    /**
     *  Log a message, with no arguments.     
     */
    public void log(Target curtarget, String msg,Throwable thrown) {            
        if ( (curtarget.intValue() & targets ) == curtarget.intValue() ) {                 
            LogRecord lr = new LogRecord(msg, curtarget);
            lr.setLoggerName(name);
            String cn = CallerStack.getCallerClass(this.getClass()).getName();            
            lr.setSourceClassName(cn);
            lr.setTarget(curtarget);           
            lr.setThrown(thrown);
            lr.setConfigurationName(configName);
            lr.setNDC(NDC.getAsString());
            long elp = -1;
            if ( TimeTracker.isTracking() ) {
                elp = TimeTracker.getEllapsedTime();
            }
            lr.setElapsed(elp);
            log(lr);
        }
    }
    
    
    /**
     *  Log a message, with associated Throwable information.          
     */
    public void log(Target target, String msg) {        
        log ( target,msg,null);        
    }
    
    
    /**
     *  Log a LogRecord.
     *     
     */
    public void log(LogRecord record) {
        for ( int i = 0; i < generators.size();i++) {
            LogGenerator lg = (LogGenerator)generators.get(i);
            if ( filter != null ) {
                if ( filter.match(record.getMessage()) ) {                    
                    processor.processEvent(lg, record);                                        
                }
            }
            else {
                processor.processEvent(lg, record);        
            }
        }                             
    }
    
    /**     
     */
    public void setTargets(int targets) {
        this.targets = targets;
    }
    
    public boolean isLoggable(Target target) {
        if ( (target.intValue() & targets ) == target.intValue() ) {
            return true;
        }
        else {
            return false;
        }
    }
    /**
     */
    public int getTargets() {
        return targets;
    }
    
    /**
     *  Sets the loggerName attribute of the Logger object
     */
    public void setLoggerName(String name) {
        this.name = name;
    }
    
    
    /**
     *  Gets the loggerName attribute of the Logger object          
     */
    public String getLoggerName() {
        return name;
    }
    
    public void trace(Object obj) {
        if ( obj != null ) {
            log(Target.trace, obj.toString(),null);
        }
    }
    
    public void trace(Object obj, Throwable throwable) {
        if ( obj != null ) {
            log(Target.trace, obj.toString(),throwable);
        }
    }
        
    public void info(Object obj) {
        if ( obj != null ) {
            log(Target.info, obj.toString(),null);
        }
    }
    
    public void info(Object obj, Throwable throwable) {
        if ( obj != null ) {
            log(Target.info, obj.toString(),throwable);
        }
    }
    
    public void debug(Object obj) {
        if ( obj != null ) {
            log(Target.debug, obj.toString(),null);
        }
    }
    
    public void debug(Object obj, Throwable throwable) {
        if ( obj != null ) {
            log(Target.debug, obj.toString(),throwable);
        }
    }
    
    public void warn(Object obj) {
        if ( obj != null ) {
            log(Target.warn, obj.toString(),null);
        }
    }
    
    public void warn(Object obj, Throwable throwable) {
        if ( obj != null ) {
            log(Target.warn, obj.toString(),throwable);
        }
    }
        
    public void error(Object obj) {
        if ( obj != null ) {
            log(Target.error, obj.toString(),null);
        }
    }
    
    public void error(Object obj, Throwable throwable) {
        if ( obj != null ) {
            log(Target.error, obj.toString(),throwable);
        }
    }
    
    public void fatal(Object obj) {
        if ( obj != null ) {
            log(Target.fatal, obj.toString(),null);
        }
    }
    
    public void fatal(Object obj, Throwable throwable) {
        if ( obj != null ) {
            log(Target.fatal, obj.toString(),throwable);
        }
    }
            
    // ------------------------------------    
    public boolean isTraceEnabled() {
        return isLoggable(Target.trace);
    }
    
    public boolean isInfoEnabled() {
        return isLoggable(Target.info);
    }
        
    public boolean isDebugEnabled() {
        return isLoggable(Target.debug);
    }
    
    public boolean isWarnEnabled() {
        return isLoggable(Target.warn);
    }
    
    public boolean isErrorEnabled() {
        return isLoggable(Target.error);
    }
    
    public boolean isFatalEnabled() {
        return isLoggable(Target.fatal);
    }
                            
    /** Getter for property filter.
     * @return Value of property filter.
     *
     */
    public LogFilter getFilter() {
        return filter;
    }
    
    /** Setter for property filter.
     * @param filter New value of property filter.
     *
     */
    public void setFilter(LogFilter filter) {
        this.filter = filter;
    }
    
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("logger[ name=");
        buffer.append(getLoggerName());
        buffer.append(", targets=");
        buffer.append(getTargets());
        buffer.append(",filter=");
        if ( filter != null ) {
            buffer.append(getFilter().getClass().getName());
        }        
        buffer.append("]");
        return buffer.toString();
    }
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.