Package org.objectweb.util.monolog.wrapper.log4jMini

Source Code of org.objectweb.util.monolog.wrapper.log4jMini.MonologCategory

/**
* Copyright (C) 2001-2003 France Telecom R&D
*
* 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 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
*/

package org.objectweb.util.monolog.wrapper.log4jMini;

import org.apache.log4j.Appender;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.objectweb.util.monolog.api.Handler;
import org.objectweb.util.monolog.api.Level;
import org.objectweb.util.monolog.api.TopicalLogger;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.wrapper.log4jMini.LevelImpl;

import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;

public class MonologCategory implements TopicalLogger {

  protected Hashtable handlers;
  protected boolean enable;
  protected Category category;
  protected OwPriority interPriority = null;

  public MonologCategory(Category category) {
    this.category = category;
    enable = true;
  }

  // IMPLEMENTATION OF INTERFACE Logger

  /**
   * Set the current level of the logger
   */
  public void setIntLevel(int level) {
    if (level == BasicLevel.INHERIT) {
      category.setPriority(null);
      return;
    }
    switch (level) {
    case 10000:
      category.setPriority(org.apache.log4j.Priority.DEBUG);
      break;
    case 20000:
      category.setPriority(org.apache.log4j.Priority.INFO);
      break;
    case 30000:
      category.setPriority(org.apache.log4j.Priority.WARN);
      break;
    case 40000:
      category.setPriority(org.apache.log4j.Priority.ERROR);
      break;
    case 50000:
      category.setPriority(org.apache.log4j.Priority.FATAL);
      break;
    default:
      if (interPriority == null)
        interPriority = new OwPriority(level, String.valueOf(level));
      else
        interPriority.level = level;
      category.setPriority(interPriority);
      break;
    }
  }

  public void setLevel(Level l) {
    if (l.getIntValue() == BasicLevel.INHERIT) {
      category.setPriority(null);
      return;
    }
    switch (l.getIntValue()) {
    case 10000:
      category.setPriority(org.apache.log4j.Priority.DEBUG);
      break;
    case 20000:
      category.setPriority(org.apache.log4j.Priority.INFO);
      break;
    case 30000:
      category.setPriority(org.apache.log4j.Priority.WARN);
      break;
    case 40000:
      category.setPriority(org.apache.log4j.Priority.ERROR);
      break;
    case 50000:
      category.setPriority(org.apache.log4j.Priority.FATAL);
      break;
    default:
      if (interPriority == null) {
        interPriority = new OwPriority(l.getIntValue(), l.getName());
      }
      else {
        interPriority.level = l.getIntValue();
      }
      category.setPriority(interPriority);
      break;
    }
  }

  /**
   *  Return the current Level of the logger
   */
  public int getCurrentIntLevel() {
    Priority p = category.getPriority();
    return (p==null ? BasicLevel.INHERIT : p.toInt());
  }

  public Level getCurrentLevel() {
    Priority p = category.getPriority();
    return (p==null
      ? BasicLevel.LEVEL_INHERIT
      : LevelImpl.getLevel(p.toInt()) );
  }

  /**
   * Check if the level parameter are not filtered by the logger
   */
  public boolean isLoggable(int level) {
    return level >= category.getChainedPriority().toInt();
  }

  public boolean isLoggable(Level l) {
    return isLoggable(l.getIntValue());
  }

  /**
   * Is the handler enabled
   */
  public boolean isOn() {
    return this.enable;
  }

  /**
   * Log an object with a specific level. If the level parameter is
   * loggable the object is handled.
   */
  public void log(int level, Object o) {
    if (this.enable && this.isLoggable(level)) {
      callLog(level, o.toString(), null);
    }
  }

  public void log(Level l, Object o) {
    log(l.getIntValue(), o.toString());
  }

  /**
   * Log an object and a trowable with a specific level.
   */
  public void log(int level, Object o, Throwable t) {
    if (this.enable && this.isLoggable(level))
      callLog(level, o, t);
  }

  public void log(Level l, Object o, Throwable t) {
    log(l.getIntValue(), o, t);
  }

  /**
   * Log an object and a trowable with a specific level. This method
   * permits to specify an object instance and a method.
   */
  public void log(int level, Object o, Object location, Object method) {
    if (this.enable && this.isLoggable(level))
      log(level, o, null, location, method);
  }

  public void log(Level l, Object o, Object location, Object method) {
    if (this.enable && this.isLoggable(l))
      log(l.getIntValue(), o, null, location, method);
  }

  /**
   * Log an object and a trowable with a specific level. This method
   * permits to specify an object instance and a method.
   */
  public void log(int level, Object o, Throwable t, Object location,
          Object method) {
    if (this.enable && this.isLoggable(level))
      callLog(level,
        location.toString() + "." + method.toString()
        + ": " + o.toString(),
        t);
  }

  public void log(Level l, Object o, Throwable t, Object location,
          Object method) {
    if (this.enable && this.isLoggable(l))
      log(l.getIntValue(), o, t, location, method);
  }

  /**
   * Enable the handler
   */
  public void turnOn() {
    this.enable = true;
  }

  /**
   * Disable the handler
   */
  public void turnOff() {
    this.enable = false;
  }

  // IMPLEMENTATION OF INTERFACE TopicalLogger
  public Handler[] getHandler() {
    if (handlers==null) {
      return new Handler[0];
    }
    Handler[] result = new Handler[handlers.size()];
    int i = 0;
    for (Enumeration e= handlers.elements(); e.hasMoreElements() ; i++) {
      result[i] = (Handler)(e.nextElement());
    }
    return result;
  }

  public String getName() {
    return category.getName();
  }

  public Handler getHandler(String hn) {
    if (handlers==null) {
      return null;
    }
    return (Handler) handlers.get(hn);
  }

  public void setName(String name) {
  }

  public void removeAllHandlers() throws Exception {
    if (handlers!=null) {
      handlers.clear();
    }
    category.removeAllAppenders();
  }

  public String getType() {
    return "logger";
  }

  public void setAdditivity(boolean a) {
    category.setAdditivity(a);
  }

  public String[] getAttributeNames() {
    return new String[0];
  }

  public boolean getAdditivity() {
    return category.getAdditivity();
  }

  public Object getAttribute(String name) {
    return null;
  }

  public String[] getTopic() {
    String[] res = new String[1];
    res[0] = category.getName();
    return res;
  }

  public Object setAttribute(String name, Object value) {
    return null;
  }

  /**
   * Add a handler in the Handler list of the topicalLogger
   */
  public void addHandler(Handler h) throws Exception {
    if (h instanceof Appender) {
      if (handlers==null) {
        handlers = new Hashtable();
      }
      category.addAppender((Appender) h);
      handlers.put(h.getName(), h);
    }
    else
      throw new Exception(
        "The type of the handler does not match with this wrapper");
  }

  /**
   * Add a topic to the topicalLogger
   */
  public void addTopic(String topic) throws Exception {
  }

  /**
   * Returns the list of the different names of the topicalLogger
   */
  public Enumeration getTopics() {
    Vector v =  new Vector(1);
    v.addElement(category.getName());
    return v.elements();
  }

  /**
   * Remove a handler from the Handler list of the topicalLogger
   */
  public void removeHandler(Handler h) throws Exception {
    if (h instanceof Appender) {
      if (handlers!=null) {
        handlers.remove(h.getName());
      }
      this.category.removeAppender((Appender) h);
    } else
      throw new Exception(
        "The type of the handler does not match with this wrapper");
  }

  /**
   * Remove a topic from the topicalLogger
   */
  public void removeTopic(String topic) throws Exception {
    throw new Exception(
      "The multiple name is not supported in this version");
  }

  private void callLog(int level, Object o, Throwable t) {
    if (level > Priority.ERROR_INT)
      this.category.fatal(o, t);
    else if (level > Priority.WARN_INT)
      this.category.error(o, t);
    else if (level > Priority.INFO_INT)
      this.category.warn(o, t);
    else if (level > Priority.DEBUG_INT)
      this.category.info(o, t);
    else
      this.category.debug(o, t);
  }

  // INNER CLASSES //
  //---------------//
  public class OwPriority extends org.apache.log4j.Priority {

    protected int level = 10000;

    public OwPriority(int l) {
      super(l, "INTER");
      level = l;
    }
    public OwPriority(int l, String n) {
      super(l, n);
      level = l;
    }

    public boolean isGreaterOrEqual(Priority pr) {
      return level >= pr.toInt();
    }
  }
}
TOP

Related Classes of org.objectweb.util.monolog.wrapper.log4jMini.MonologCategory

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.