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

📄 log.java

📁 一个MMORPG手机游戏的服务器端程序源代码
💻 JAVA
字号:
package zsw_mmorpg.log;

import java.io.*;
import java.text.*;
import java.util.*;

/**
 * 功能说明:
 *  每天生成一个以年月日为名的LOG文件
 *  记录一些错误信息,警告信息,注释信息
 *
 */ 
public class Log {
  private static String lf = System.getProperty("line.separator", "\n");

  // true if filename is valid and writeable
  private static boolean isValid = true;
  // filename for log file
  //ivate static FileWriter logfile = null;
  private static OutputStreamWriter logfile = null;
  // Levels - Notice, Warning, Error
  private static final String NOTICE = "NOTICE ";
  private static final String WARNING = "WARNING";
  private static final String ERROR = " ERROR ";
  private static String createLogDate = "";

  //级别:0窗口上正常显示初始化数据;1正常跟踪显示;2调试跟踪显示
  private static int level = sysproperty.getInt("log.level", 1);

  private static OutputStreamWriter systemOut = null;

  static {

    init();
    try {
      systemOut = new OutputStreamWriter(System.out, "GB2312");
    }
    catch (Exception e) {
      System.out.println(e);
      systemOut = new OutputStreamWriter(System.out);
    }
  }

  private static String getDateString() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    String s2 = formatter.format(new Date());
    return s2;
  }

  /**
   * points Log file at the given path/filename.
   */
  private static void init() {
    try {
      String logFileString = (String) sysproperty.getString("log.path",
          "../logs/");
      if (logFileString == null) {
        throw new Exception("");
      }
      createLogDate = getDateString();
      logFileString += createLogDate + ".log";
      logfile = new OutputStreamWriter(new FileOutputStream(logFileString, true),
                                       "GB2312");
    }
    catch (Exception e) {
      e.printStackTrace();
      isValid = false;
    }
    catch (Throwable t) {
      isValid = false;
    }
  }

  /*
   * Makes a log entry at the NOTICE level
   *
   * @returns true if entry was entered, false otherwise
   */
  public static boolean note(String description) {
    return log(NOTICE, description);
  }

  /*
   * Makes a log entry at the WARNING level
   *
   * @returns true if entry was entered, false otherwise
   */
  public static boolean warn(String description) {
    return log(WARNING, description);
  }

  /*
   * Makes a log entry at the ERROR level
   *
   * @returns true if entry was entered, false otherwise
   */
  public static boolean error(String description) {
    return log(ERROR, description);
  }

  /*
   * Makes a log entry at the ERROR level
   *
   * @returns true if entry was entered, false otherwise
   */
  public static boolean error(String description, Throwable t) {
    return log(ERROR, description, t);
  }

  /*
   * Makes a log entry at the ERROR level
   *
   * @returns true if entry was entered, false otherwise
   */
  public static boolean error(Throwable t) {
    return log(ERROR, "", t);
  }

  public static void setDebugLevel(int levels) {
    if (level < 0) {
      level = 0;
    }
    else {
      level = levels;
    }
  }

  /**
   * 调试跟踪
   */
  public static void debug(String description, int level) {
    debug(description, null, level);
  }

  public static void debug(String description, Throwable t, int level) {
    print(description, t, level);
  }

  public static void print(String descripttion, Throwable t, int levels) {
    if (level >= levels) {
      log("DEBUG" + level, descripttion, t);
    }
  }

  private static boolean log(String level, String description) {
    return log(level, description, null);
  }

  private static StringBuffer getPrintBuffer(String level, String description,
                                             Throwable t) {

    Date date = new Date();
    StringBuffer logEntry = new StringBuffer();
    logEntry.append("[");
    logEntry.append(date.toString());
    logEntry.append("] -- ");
    logEntry.append(level);
    logEntry.append(" -- ");
    logEntry.append(description);
    logEntry.append(lf);
    if (t != null) {
      ByteArrayOutputStream ostr = new ByteArrayOutputStream();
      PrintWriter out = new PrintWriter(ostr, true);
      out.write(logEntry.toString());
      out.write("\tException:  ");
      out.write(t.toString());
      out.write(lf + "\tStack Trace follows:" + lf + "\t");
      t.printStackTrace(out);
      logEntry = new StringBuffer(ostr.toString());
    }
    return logEntry;

  }

  private static boolean log(String level, String description, Throwable t) {
    if (isValid) {
      StringBuffer logEntry = getPrintBuffer(level, description, t);
      synchronized (logfile) {
        try {
          if (!createLogDate.startsWith(getDateString())) {
            logfile.close();
            init();
          }
          //
          CharArrayWriter buf = new CharArrayWriter();
          PrintWriter writer = new PrintWriter(buf);
          writer.write(logEntry.toString());
          buf.writeTo(logfile);
          logfile.flush();
        }
        catch (IOException ioe) {
          // ignore, skip log, should not occur since we have checked file
          // but catching the exception allows Log to be used elsewhere in
          // catch blocks.
          return false;
        }
      }
      return true;
    }
    return false;
  }

  /**
      Attempt to close the log file when the class is GC'd
   */
  public void destroy() {
    try {
      logfile.close();
    }
    catch (Exception e) {}
  }

  /**
   *
   */
  public static void print(String s) {
    try {
      CharArrayWriter buf = new CharArrayWriter();
      PrintWriter writer = new PrintWriter(buf);
      writer.write(s);
      buf.writeTo(systemOut);
      systemOut.flush();
    }
    catch (Exception e) {}
  }

  public static void println(String method, String s, int nlevel) {
    try {
      //System.out.println( s );
      if (nlevel <= level) {
        print(s + lf);
      }
    }
    catch (Exception e) {}
  }

  public static void println(String method, Throwable t, int nlevel) {
    if (nlevel <= level) {
      System.out.println(t);
      //t.printStackTrace( systemOut );
    }
  }

  /**
   *
   */
  public static void print(String from, String to, String serviceid,
                           String method, Throwable t, int nlevel) {
    if (nlevel <= level) {
      System.out.println(t);
    }
  }

  public static void print(String from, String to, String serviceid,
                           String method, String s, int nlevel) {

    try {
      if (nlevel <= level) {

        //System.out.println( s );
        systemOut.write(s);
      }
    }
    catch (Exception e) {}
  }

  /**
   *
   */
  public static void main(String argv[]) {
    Log.setDebugLevel(3);
    Log.println("MAIN", "中国-你好", 0);
    Log.note("中国AB");
  }
}

⌨️ 快捷键说明

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