📄 log.java
字号:
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: lutong Copyright (c) 2004</p>
* <p>Company: www.lazybug.com</p>
* @author 刘学
* @version 0.5
*/
package org.lazybug.util;
import java.util.*;
import java.io.*;
public class Log implements Runnable
{
public static final int _FILE_SIZE = 10*1024*1024;
private static Log handle;
private LinkedList queue;
private boolean writable;
private FileOutputStream out = null;
public static Log getInstance()
{
if( handle == null )
handle = new Log();
return handle;
}
private Log()
{
queue = new LinkedList();
writable = false;
}
public void start()
{
Thread thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
public boolean isWritable(){
return this.writable;
}
public void run()
{
long count = 0;
writable = true;
Log.logMessage(this, "Logger listener start!The log can be written.");
StringBuffer sb = new StringBuffer();
try
{
while( true )
{
synchronized( this )
{
if( this.queue.isEmpty() ){
try{
this.wait();
}catch( InterruptedException e ){}
}
}
try
{
//System.out.println(ConfigUtil.APP_PATH+"log\\");
File file = new File(ConfigUtil.APP_PATH+"log\\"+
Tools.getFormatTime("yyyy-MM-dd",System.currentTimeMillis())+".txt");
out = new FileOutputStream( file, true );
}
catch(java.io.FileNotFoundException e)
{
Log.logError(this, "Fail to open log file.");
return;
}
String szLog = null;
while( (szLog=peek()) != null )
{
sb.append(szLog);
}
byte[] buffer = sb.toString().getBytes();
out.write( buffer );
out.flush();
out.close();
count += buffer.length;
sb.delete(0, sb.length());
}
}
catch(IOException e)
{
Log.logError(this, "写日志失败,日志文件被关闭,请尝试重新打开!");
}
finally
{
try{
if( out != null ) out.close();
}catch(IOException e)
{}
}
}
/**
* 向文件写数据
* @param log
*/
public synchronized void write(String log)
{
if( this.writable )
{
this.queue.addLast( log );
this.notify();
}
}
private synchronized String peek()
{
try
{
String szLog = this.queue.removeFirst().toString();
//String header = "["+Tools.getFormatTime("HH:mm:ss", System.currentTimeMillis())+"] ";
return szLog + "\r\n";
}
catch (NoSuchElementException nee)
{
return null;
}
}
public static void logMessage(Object obj, String log)
{
String content;
if( obj != null )
content = "["+Tools.getFormatTime("yyyy-MM-dd HH:mm:ss",System.currentTimeMillis())+"] "+
obj.getClass().getName()+"\n信息:"+log+"\n";
else
content = "["+Tools.getFormatTime("yyyy-MM-dd HH:mm:ss",System.currentTimeMillis())+
"] \n信息:"+log+"\n";
System.out.println(content);
Log.getInstance().write(content);
}
public static void logWarning(Object obj, String log)
{
String content;
if( obj != null )
content = "["+Tools.getFormatTime("yyyy-MM-dd HH:mm:ss",System.currentTimeMillis())+"] "+
obj.getClass().getName()+"\n警告:"+log+"\n";
else
content = "["+Tools.getFormatTime("yyyy-MM-dd HH:mm:ss",System.currentTimeMillis())+
"] \n警告:"+log+"\n";
System.out.println(content);
Log.getInstance().write(content);
}
public static void logError(Object obj, String log)
{
String content;
if( obj != null )
content = "["+Tools.getFormatTime("yyyy-MM-dd HH:mm:ss",System.currentTimeMillis())+"] "+
obj.getClass().getName()+"\n错误:"+log+"\n";
else
content = "["+Tools.getFormatTime("yyyy-MM-dd HH:mm:ss",System.currentTimeMillis())+
"] \n错误:"+log+"\n";
System.out.println(content);
Log.getInstance().write(content);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -