简体   繁体   English

Java-实现日志记录代码时出错

[英]Java - error implementing logging code

I want to implement OSGI bundle which can write error messages into log file. 我想实现可以将错误消息写入日志文件的OSGI捆绑软件。 I have some some errors in the code that I cannot solve. 我的代码中有些错误无法解决。 I have commented the code code where Netbeans give me errors. 我已经注释了Netbeans给我错误的代码。

public class LoggingSystemImpl implements LoggingSystem { 公共类LoggingSystemImpl实现LoggingSystem {

   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() {};

error message in Netbeans: is not abstract and does not override abstract method getPrintStream() in sun.rmi.runtime.Log Netbeans中的错误消息:不是抽象的,并且不会覆盖sun.rmi.runtime.Log中的抽象方法getPrintStream()

            closed = false;
            log.writeln("logged in.");

error message: cannot find symbol symbol: method writeln(java.lang.String) location: variable log of type sun.rmi.runtime.Log 错误消息:找不到符号符号:方法writeln(java.lang.String)位置:类型为sun.rmi.runtime.Log的变量日志

log.nl();

error message: cannot find symbol symbol: method nl() location: variable log of type sun.rmi.runtime.Log 错误消息:找不到符号符号:方法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 + "]";
    }





}

Unless there's some reason why you need to create your own logging mechanism I would use log4j . 除非出于某些原因需要创建自己的日志记录机制,否则我将使用log4j It's fairly simple to configure and get working properly and would save you a lot of time. 配置和正常工作非常简单,可以节省大量时间。

I'm almost certain you are using the wrong Log class. 我几乎可以确定您使用了错误的Log类。 Anything in sun.* packages is not for user consumption. sun。*软件包中的任何内容都不供用户使用。 If you want logging for your code, use the java.util.logging library or log4j. 如果要记录代码,请使用java.util.logging库或log4j。 Or maybe OSGI provides a framework. 也许OSGI提供了一个框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM