Package gederedem

Source Code of gederedem.Trace$LogFormat

/**
*
*/
package gederedem;

import java.io.IOException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.MissingResourceException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
* Personalized logger
* @author chrelaix
*
*/
public class Trace extends Logger {
  // Constants
  //--------------------------
  private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
  private ConsoleHandler console = new ConsoleHandler();
  private FileHandler lfh;
  private String logfile;
 
  public static final Level DEBUG = Level.FINE;
  public static final Level BIGDEBUG = Level.FINER;
 
  // Constructors
  //--------------------------
  public Trace(String name, String resourceBundleName) {
    super(name, resourceBundleName);
    // By default log with normal format
    LogFormat lf = new LogFormat();
    console.setFormatter(lf);
    addHandler(console);
  }

  // Getters and Setters
  //--------------------------
  /**
   * @return the logfile.
   */
  @SuppressWarnings("unused")
  private String getLogfile() {
    return logfile;
  }

  /**
   * @param logfile the logfile to set.
   */
  @SuppressWarnings("unused")
  private void setLogfile(String file) {
    if (lfh != null) {
      removeHandler(lfh);
    }

    this.logfile = file;
    // TODO : if lfh already exist destroy it before recreate it
    try {
      //%g = 9 files with No automatically inserted
      // TODO : variabilize the size of logger
      lfh = new FileHandler(file + "%g.log",2000,9);
      lfh.setFormatter(new LogFormat());
      this.addHandler(lfh);
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /* (non-Javadoc)
   * @see java.util.logging.Logger#setLevel(java.util.logging.Level)
   */
  @Override
  public void setLevel(Level newLevel) throws SecurityException {
    super.setLevel(newLevel);
    Formatter newFormatter = new LogFormat();
    console.setLevel(newLevel);
    console.setFormatter(newFormatter);
    if (logfile != null) {
      try {
        // Stop to log in old file
        removeHandler(lfh);
        lfh = new FileHandler(logfile);
        lfh.setFormatter(newFormatter);
        addHandler(lfh);
      } catch (IOException e) {
        e.printStackTrace();
        severe(e.getLocalizedMessage());
      }
    }
  }

  // Methods
  //--------------------------
  /**
   * Get the message in localized format.
   * @param msgId the message name
   */
  public String getMsg(String msgId) {
    return getResourceBundle().getString(msgId);
  }

  /* (non-Javadoc)
   * @see java.util.logging.Logger#severe(java.lang.String)
   */
  @Override
  public void severe(String msg) {
    super.severe(msg);
  }

  /* (non-Javadoc)
   * @see java.util.logging.Logger#warning(java.lang.String)
   */
  @Override
  public void warning(String msg) {
    try {
      super.warning(msg);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }

  /* (non-Javadoc)
   * @see java.util.logging.Logger#info(java.lang.String)
   */
  @Override
  public void info(String msg) {
    try {
      super.info(msg);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }

  /**
   * Debug trace.
   * @param msg the log
   */
  public void debug(String msg) {
    try {
      super.fine(msg);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }

  /**
   * More debug trace.
   * @param msg the log
   */
  public void bigDebug(String msg) {
    try {
      super.finer(msg);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }

  /**
   * All trace.
   * @param msg the log
   */
  public void allDebug(String msg) {
    try {
      super.finest(msg);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }

  /**
   * Trace for entering function.
   * @param cl the class who log the trace
   * @param meth the method who log the trace
   */
  public void entering(String cl, String meth) {
    try {
      bigDebug(getMsg("logs.code.enterMethod") + " " + cl + "/" + meth);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }
 
  /**
   * Trace for exiting function.
   * @param cl the class who log the trace
   * @param meth the method who log the trace
   */
  public void exiting(String cl, String meth) {
    try {
      bigDebug(getMsg("logs.code.exitMethod") + " " + cl + "/" + meth);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }

  /**
   * Trace for exiting function.
   * @param cl the class who log the trace
   * @param meth the method who log the trace
   * @param ret the return value
   */
  public void exiting(String cl, String meth, String ret) {
    try {
      bigDebug(getMsg("logs.code.exitMethod") + " " + cl + "/" + meth + ", " +
          getMsg("logs.code.returnValue") + ret);
    } catch (MissingResourceException ex) {
      severe(ex.getStackTrace().toString());
    }
  }
 
  /**
   * Class to format a line in log as :<br>
   * [date hour - class (loglevel)] log message
   *
   * @author chrelaix
   *
   */
  public static class LogFormat extends Formatter {

    @Override
    public String format(LogRecord log) {
      String formattedLog = "[" +
          df.format(new Date(log.getMillis())) + " - " +
          "(" + log.getLevel() + ")" +
          "] " +
          formatMessage(log) + "\n";
      return formattedLog;
    }
  }
}
TOP

Related Classes of gederedem.Trace$LogFormat

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.