gpt4 book ai didi

Java - 实现日志代码时出错

转载 作者:行者123 更新时间:2023-12-02 00:27:39 27 4
gpt4 key购买 nike

我想实现 OSGI 包,它可以将错误消息写入日志文件。我的代码中有一些无法解决的错误。我已经注释了 Netbeans 给我错误的代码。

公共(public)类 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() {};

Netbeans 中的错误消息:不是抽象的,并且不会覆盖 sun.rmi.runtime.Log 中的抽象方法 getPrintStream()

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

错误消息:找不到符号 符号:方法 writeln(java.lang.String) location:sun.rmi.runtime.Log类型的变量日志

log.nl();

错误消息:找不到符号 符号:方法 nl() location: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 + "]";
}





}

最佳答案

除非出于某种原因您需要创建自己的日志记录机制,否则我会使用 log4j 。配置和正常工作相当简单,并且可以节省您大量时间。

关于Java - 实现日志代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9690701/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com