logger.java

来自「java xml开发指南(初学者推荐)Java Xml 编程指南书籍源码」· Java 代码 · 共 65 行

JAVA
65
字号
package MyNa.utils;
// logger class

import java.io.*;

public class Logger { // may be shared across threads.
  static String fileProp="MyNa.utils.Logger.file";
  static String debugProp="MyNa.utils.Logger.debugLevel";
  static String defaultFileName="dbLog.log";
  String fileName;
  int debugLevel; // simply on or off, in this version;
        // we use separate Logger objects for err, admin.

public Logger(String fName){
  fileName=fName;
  debugLevel=Integer.parseInt(System.getProperty(debugProp,"1"));
  }

public Logger(){this(System.getProperty(fileProp,defaultFileName));}

public synchronized void setFileName(String fName){fileName=fName;}
public synchronized void setDebugLevel(int N){debugLevel=N;}
public synchronized void setDebug(boolean B){debugLevel=B?1:0;}
public synchronized void setDebug(String S){
  setDebug((new Boolean(S)).booleanValue());
}

public synchronized void clearLog(){ 
  if(debugLevel<=0)return;
  try{
    PrintWriter f=new PrintWriter(new FileWriter(fileName,true));
    String S=MiscDate.todaysDate();
    f.println(S);
    f.close();
  }catch(IOException e){}
}

public synchronized void logIt(String S){
  if(debugLevel<=0)return;
  try{
    PrintWriter f=new PrintWriter(new FileWriter(fileName,true));
    f.println(S);
    f.close();
    }catch(IOException e){}
}
public synchronized void logIt(String S,Throwable ex){ 
  if(debugLevel<=0)return;
  try{
    PrintWriter f=new PrintWriter(new FileWriter(fileName,true));
    f.println(S);
    ex.printStackTrace(f);
    f.close();
    }catch(IOException e){}
}

public static String stackTrace(Throwable ex){
    StringWriter sw=new StringWriter();
    ex.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

}


⌨️ 快捷键说明

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