Package net.sourceforge.javautil.common.logging.standard

Source Code of net.sourceforge.javautil.common.logging.standard.LoggingFrameworkWriter

package net.sourceforge.javautil.common.logging.standard;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sourceforge.javautil.common.DateUtil;
import net.sourceforge.javautil.common.ThrowableUtil;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.logging.ILogger;
import net.sourceforge.javautil.common.logging.ILoggerLevel;
import net.sourceforge.javautil.common.logging.LoggerLevelStandard;
import net.sourceforge.javautil.common.logging.LoggingContext;
import net.sourceforge.javautil.common.logging.ILoggingFramework;
import net.sourceforge.javautil.common.logging.LoggingFrameworkAbstract;
import net.sourceforge.javautil.common.logging.LoggingFrameworkRouter;
import net.sourceforge.javautil.common.logging.jdk.LoggingFrameworkJDK;

/**
* A simple framework backed by a {@link Writer}.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class LoggingFrameworkWriter extends LoggingFrameworkAbstract<String, LoggerWriter> implements LoggingFrameworkRouter {
 
  public static final String DEFAULT_ROOT_LEVEL = "net.sf.logging.writer.level.default";
 
  /**
   * @param stream The stream for the {@link LoggingFrameworkWriter}
   * @param level The level to use initially
   *
   * @return The writer based logging framework
   */
  public static LoggingFrameworkWriter standardSetup (OutputStream stream, LoggerLevelStandard level) {
    LoggingFrameworkWriter lfw = new LoggingFrameworkWriter(new PrintWriter(stream, true));
    lfw.setRootLevel(level);
    new LoggingContext(lfw).setGlobal();
    LoggingFrameworkJDK.getInstance().routeTo(lfw);
    return lfw;
  }
 
  protected final Writer writer;
 
  protected LoggerLevelStandard rootLevel = LoggerLevelStandard.FATAL;
  protected SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss ZZZ");

  public LoggingFrameworkWriter(Writer writer) {
    this.writer = writer;
   
    String defaultLevel = System.getProperty(DEFAULT_ROOT_LEVEL);
    if (defaultLevel != null) {
      this.rootLevel = LoggerLevelStandard.valueOf(defaultLevel.toUpperCase());
    }
  }

  /**
   * @return The level for filtering logging
   */
  public LoggerLevelStandard getRootLevel() { return rootLevel; }
  public void setRootLevel(LoggerLevelStandard rootLevel) { this.rootLevel = rootLevel; }

  /**
   * @return The format used for formatting dates
   */
  public SimpleDateFormat getDateFormat() { return dateFormat; }
  public void setDateFormat(SimpleDateFormat dateFormat) { this.dateFormat = dateFormat; }

  public LoggingFrameworkRouter getRouter() { return this; }

  public void route(String loggerName, long millis, ILoggerLevel level, String message, Throwable throwable) {
    this.log(getLogger(loggerName), new Date(millis), level, message, throwable);
  }

  @Override protected LoggerWriter createLogger(String name) {
    return new LoggerWriter(this, name, name.contains(".") ? name.substring(name.lastIndexOf('.')+1) : name);
  }

  public void initialize(String configuration) {}

  public void reconfigure(String configuration) {}

  /**
   * @param level The level in question
   * @return True if logging is actually taking place for the level, otherwise false
   */
  public boolean isLogging (ILoggerLevel level) {
    return level.getBasicType().compareTo(rootLevel.getBasicType()) >= 0;
  }
 
  /**
   * @param level The level
   * @param message The message to log
   * @param throwable The exception
   */
  public void log (ILogger logger, Date date, ILoggerLevel level, String message, Throwable throwable) {
    if (!isLogging(level)) return;
    try {
      writer.write("[");
      writer.write(dateFormat.format(date));
      writer.write("] [");
      writer.write(logger.getName());
      writer.write("] [");
      writer.write(level.getName());
      writer.write("] ");
      writer.write(message);
      writer.write("\n");
      if (throwable != null) writer.write(ThrowableUtil.toString(throwable));
      writer.flush();
    } catch (IOException e) {
      throw ThrowableManagerRegistry.caught(e);
    }
  }

}
TOP

Related Classes of net.sourceforge.javautil.common.logging.standard.LoggingFrameworkWriter

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.