2012-03-13 47 views
0

我想實現可以將錯誤消息寫入日誌文件的OSGI bundle。我在代碼中存在一些我無法解決的錯誤。我評論了Netbeans給我錯誤的代碼。Java - 執行日誌代碼的錯誤

公共類LoggingSystemImpl實現LoggingSystem {Netbeans中

private final static Calendar calendar = Calendar.getInstance(); 
    private final static String user = System.getenv("USERNAME").toLowerCase(); 
    private final static String sMonth = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH); 
    private final static int y = calendar.get(Calendar.YEAR); 

    // the name of the log file 
    //private final String logName = sysDrive + "\\fttb_web - " + sMonth.toLowerCase() + ", " + y + ".log"; 
    private final String logName = "logger - " + sMonth.toLowerCase() + ", " + y + ".log"; 

    private static boolean closed; 
    private static Log log = null; 
    private static BufferedWriter bw = null; 
    private static FileOutputStream fos = null; 
    private static OutputStreamWriter osw = null; 

    public LoggingSystemImpl() { 
    } 


    public String LoggingSystemUtilization() throws FileNotFoundException{ 


     return "ok"; 
    } 


    private String Log() throws IOException 
{ 
    fos = new FileOutputStream(logName, true); 

    // set encoding to cyrillic (if available) 
    if (Charset.isSupported("windows-1251")) 
    { 
     osw = new OutputStreamWriter(fos, Charset.forName("windows-1251")); 
    } 
    else { osw = new OutputStreamWriter(fos); } 

    bw = new BufferedWriter(osw, 2048); // 2Mb buffer 

    return"ok"; 

} 

// intro header for log session 
public static synchronized Log getInstance() throws IOException 
{ 
    boolean exc = false; 
    try 
    { 
     if (log == null || closed) 
     { 
      log = new Log() {}; 

錯誤消息:不是抽象和sun.rmi.runtime.Log

不覆蓋抽象方法getPrintStream()
  closed = false; 
      log.writeln("logged in."); 

錯誤message:找不到符號 symbol:方法writeln(java.lang.String) location:sun.rmi.runtime.Log類型的變量日誌

log.nl(); 

錯誤消息:找不到符號 代碼:NL法() 位置:類型的變量數sun.rmi.runtime.Log

  } 
     } 
     catch(IOException x) { exc = true; throw x; } 
     catch(Exception x) { exc = true; x.printStackTrace(); } 
     finally 
     { 
      if (exc) 
      { 
       try 
       { 
        if (fos != null) { fos.close(); fos = null; } 
        if (osw != null) { osw.close(); fos = null; } 
        if (bw != null) { bw.close(); bw = null; } 
       } 
       catch(Exception x) { x.printStackTrace(); } 
      } 
     } 
     return log; 
    } 


    public synchronized void nl() 
    { 
     try { bw.newLine(); } 
     catch(IOException x) {x.printStackTrace();} 
    } 

    public synchronized void nl(int count) 
    { 
     try 
     { 
      for (int i = 0; i < count; i++) bw.newLine(); 
     } 
     catch(IOException x) {x.printStackTrace();} 
    } 
    public synchronized void writeln(String s) 
    { 
     try { bw.write(getTime() + ": " + s); bw.newLine(); } 
     catch(IOException x) {x.printStackTrace();} 
    } 

    public synchronized void write(String s) 
    { 
     try { bw.write(s); } 
     catch (IOException x) {x.printStackTrace();} 
    } 

    public synchronized void close() 
    { 
     try 
     { 
      if (bw != null) 
      { 
       writeln("logged out."); 
       nl(); 
       bw.flush(); 
       bw.close(); 
       closed = true; 

       fos = null; 
       osw = null; 
       bw = null; 
      } 
     } 
     catch(IOException x) { x.printStackTrace(); } 

    } 

    public synchronized boolean isClosed() { return closed; } 

    public synchronized void writeException(Exception x) 
    { 
     writeln(""); 
     write("\t" + x.toString()); nl(); 
     StackTraceElement[] ste = x.getStackTrace(); 
     int j = 0; 
     for (int i = 0; i < ste.length; i++) 
     { 

      if (i < 15) { write("\t\tat " + ste[i].toString()); nl(); } 
      else { j++; } 

     } 

     if (j > 0) { write("\t\t... " + j + " more"); nl(); } 

     nl(2); 
    } 

    private String getTime() 
    { 
     Calendar c = Calendar.getInstance(); 
     int month = c.get(Calendar.MONTH) + 1; 

     int d = c.get(Calendar.DAY_OF_MONTH); 
     int h = c.get(Calendar.HOUR_OF_DAY); 

     int m = c.get(Calendar.MINUTE); 
     int s = c.get(Calendar.SECOND); 
     int y = c.get(Calendar.YEAR); 

     String dd = d < 10 ? "0"+d : ""+d; 
     String hh = h < 10 ? "0"+h : ""+h; 
     String mm = m < 10 ? "0"+m : ""+m; 
     String ss = s < 10 ? "0"+s : ""+s; 
     String sm = month < 10 ? "0"+month : ""+month; 

     return user + " [" + y + "." + sm + "." + dd + " " + hh + ":" + mm + ":" + ss + "]"; 
    } 





} 

回答

1

我幾乎可以肯定你正在使用錯誤的Log類。任何在陽光下。*包不適合用戶消費。 如果您想要記錄代碼,請使用java.util.logging庫或log4j。或者也許OSGI提供了一個框架。

2

除非有一些原因,你需要創建自己的日誌機制我會用log4j。 這很簡單,配置和正常工作,並會爲您節省大量的時間。