⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 standardlogger.java

📁 数据仓库工具
💻 JAVA
字号:
/**
    Copyright (C) 2002-2003  Together
    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.1 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.webdocwf.util.loader.logging;

import java.util.*;
import java.io.*;
import org.webdocwf.util.loader.BufferOctopusClass;

/**
 * Standard implementation of the <CODE>Logger</CODE>.  This is
 * general-purpose logging facility.  A client that needs additional
 * functionality can either extend this class or provide there own
 * implementationm of <CODE>Logger</CODE>. <P>
 *
 * Currently this is a bare-bones class that writes INFO and above
 * levels to stderr and all others to a log file.
 *
 * @author Sinisa Milosevic
 * @see org.webdocwf.util.loader.logging.Logger
 */
public class StandardLogger
    extends Logger {

  /**
   * Log file name.
   */
  File activeLogFile;
  
  /**
   * Log dir
   */
  File logDir;

  /**
   * Log file writter.
   */
  PrintWriter logFileStream;

  private boolean[] enabledLogLevels = new boolean[3];
  private Hashtable messages = new Hashtable();
  private String logMode;
  public RandomAccessFile randomLoggerFile;

  /**
   * Construct a new logger.  Configuration is not done now, to allow
   * the logger to be created very early.
   */
  public StandardLogger() {
    centralLogger = this;
    this.enabledLogLevels[0] = false;
    this.enabledLogLevels[1] = false;
    this.enabledLogLevels[2] = false;
  }

  /**
   * Configure Logger with given config file, interpreting of config file is
   * logger implementation specific.
   *
   * @param confFilePath Path to configuration file.
   */
  public void configure(String confFilePath) throws Exception {
    int num = confFilePath.indexOf(";");
    String logDir = confFilePath.substring(0, num);
    String fileName = confFilePath.substring(num + 1);

    String strPi;
    Calendar calendar = Calendar.getInstance();
    Date currentDate = new Date();
    calendar.setTime(currentDate);
    int year, month, iDate, iDay, hours, minutes, seconds;
    int y, h, min, s;
    y = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH);
    month = month + 1;
    String strMonth = null;
    if (month < 10)
      strMonth = "0" + month;
    else
      strMonth = "" + month;

    String[] months = new String[] {
                      "January", "February", "March", "April", "May", "June", "July",
                      "August", "September", "October", "November", "December"
    };
    iDate = calendar.get(Calendar.DAY_OF_MONTH);
    String strDate = "";
    if (iDate < 10)
      strDate = "0" + iDate;
    else
      strDate = "" + iDate;

    iDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
    String[] days = new String[] {
                    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                    "Saturday"
    };
    hours = calendar.get(Calendar.HOUR_OF_DAY);
    String strHours = "";
    if (hours < 10)
      strHours = "0" + hours;
    else
      strHours = "" + hours;

    h = hours + 1;

    minutes = calendar.get(Calendar.MINUTE);
    String strMinutes = "";
    if (minutes < 10)
      strMinutes = "0" + minutes;
    else
      strMinutes = "" + minutes;

    h = hours + 1;

    min = minutes + 1;
    seconds = calendar.get(Calendar.SECOND);
    String strSeconds = "";
    if (seconds < 10)
      strSeconds = "0" + seconds;
    else
      strSeconds = "" + seconds;

    s = seconds + 1;
    if (fileName.equalsIgnoreCase("default")){
    
      strPi = "LoaderLog" + y + "-" + strMonth + "-" + strDate
              + "-" + strHours + "-" + strMinutes + "-" + strSeconds + ".txt";
    }else if(fileName.equalsIgnoreCase("defaultGenerator")){
			strPi = "GeneratorLog" + y + "-" + strMonth + "-" + strDate
					 + "-" + strHours + "-" + strMinutes + "-" + strSeconds + ".txt";
    }
    else
      strPi = fileName;
    try {
    	File filea = new File(logDir);
		this.logDir = filea;
//      if (!filea.exists())
//        filea.mkdirs();
      filea = new File(filea.getAbsolutePath(), strPi);
//      randomLoggerFile = new RandomAccessFile(filea, "rw");
      this.activeLogFile = filea.getAbsoluteFile();
//      write("normal", "Created: " + strPi);
//      randomLoggerFile.writeBytes("Date: " + days[iDay] + ", " + iDate + ". " +
//          months[month - 1] + " " + y + ".\n" + "Time: " + hours + ":"
//          + minutes + ":" + seconds + "\n");

    }
    catch (Exception ex) {
      throw new Exception("Cannot configure StandardLogger:" + ex.getMessage());
    }

  }

  public int getLevel(String level) {
    if (level.equalsIgnoreCase(Logger.strLOGMODE_NONE))
      return 0;
    else if (level.equalsIgnoreCase(Logger.strLOGMODE_NORMAL))
      return 1;
    else
      return 2;
  }

  public boolean isEnabled(int level) {
    boolean[] enabledLevelsValue = this.getEnabledLogLevels();
    if (enabledLevelsValue[level] == true) {
      return true;
    }
    return false;
  }

  public boolean isEnabled(String level) {
    return isEnabled(this.getLevel(level));
  }

  public void write(int level, String msg) {
    if (isEnabled(level)) {
      try {
        if (this.activeLogFile != null) {
			//create directory structure
			 if(!this.logDir.exists())
			  this.logDir.mkdirs();
        	 //create random access file
        	 if( this.randomLoggerFile == null )
				this.randomLoggerFile = new RandomAccessFile(this.activeLogFile, "rw");
			 //write
          randomLoggerFile.seek(randomLoggerFile.length());
          System.out.println(msg + "\n");
          BufferOctopusClass.getInstance().writeToBuffer(msg + "\n");
          randomLoggerFile.writeBytes(msg + "\n");
        }
      }
      catch (Exception e) {
        BufferOctopusClass.getInstance().writeToBuffer(e.getMessage() + "\n");
        e.printStackTrace();
      }
    }
  }

  public synchronized void write(String level, String msg) {
    write(getLevel(level), msg);
  }

  public synchronized void write(int level, String msg, Throwable throwable) {
    if (isEnabled(level)) {
      Date date = new Date();
      StringWriter stackBuf = new StringWriter();
      throwable.printStackTrace(new PrintWriter(stackBuf));
      stackBuf.flush();

      String errMsg = msg + ":" + " " + throwable.getMessage() + '\n' + stackBuf;
      this.write(level, errMsg);
    }
  }

  public synchronized void write(String level, String msg, Throwable throwable) {
    write(getLevel(level), msg, throwable);
  }

  public void setEnabledLogLevels(String logMode) {
    this.logMode = logMode;
    int level = this.getLevel(logMode);
    if (level == 0) {
      this.enabledLogLevels[0] = false;
      this.enabledLogLevels[1] = false;
      this.enabledLogLevels[2] = false;
    } else if (level == 1) {
      this.enabledLogLevels[0] = true;
      this.enabledLogLevels[1] = true;
      this.enabledLogLevels[2] = false;
    } else {
      this.enabledLogLevels[0] = true;
      this.enabledLogLevels[1] = true;
      this.enabledLogLevels[2] = true;
    }
  }

  public boolean[] getEnabledLogLevels() {
    return enabledLogLevels;
  }

  public String getMessage(String key) {
    if (key != null) {
      return (String)this.messages.get(key);
    } else
      return null;
  }

  public boolean setMessage(String key, String value) {
    if (value != null && key != null) {
      this.messages.put(key, value);
      return true;
    } else
      return false;
  }

  public boolean writeEcho(String strLogTxt) {
    if (!this.logMode.equalsIgnoreCase(Logger.strLOGMODE_NONE)) {
      this.write(Logger.strLOGMODE_NORMAL, strLogTxt);
      BufferOctopusClass.getInstance().writeToBuffer(strLogTxt);
      return true;
    } else
      return false;
  }

  public void close() {
    try {
      if(this.activeLogFile!=null)
        this.activeLogFile = null;
      this.randomLoggerFile.close();
    }
    catch (Exception e){
      BufferOctopusClass.getInstance().writeToBuffer(e.getMessage());
    }
  }
  


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -