📄 datalogger.java
字号:
/*
* jRevProxy, an opensource Java reverse proxy server
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* $Id: DataLogger.java,v 2.5 2003/03/27 15:49:32 fnoe Exp $
*/
package cx.noe.jrevproxy.logging;
import java.io.*;
import java.util.*;
/**
*
* DataLogger
*
* @author <a href=mailto:frederik@noe.cx > Frederik Noe</a>
* @version <tt>$Revision: 2.5 $</tt>
*
*/
public class DataLogger implements ILog {
private String logPath = null;
public final int DEBUG = 0;
public final int METHOD = 1;
public final int INFO = 2;
private int level = INFO;
private boolean debug = false;
/**
* Creates a DataLogger instance
* Every day a new logfile will be created
*
* @param logpath path where the DataLogger can create its logfiles
*
*/
public DataLogger(String logpath) throws IllegalArgumentException {
if(logpath == null || logpath.length() == 0)
throw new IllegalArgumentException("A logpath cannot be null or empty");
logPath = new String(logpath);
}
/**
* Returns the logpath
*
* @return the logpath string
*/
public String getLogPath() {
return logPath;
}
public void setDebugMode(boolean mode) {
debug = mode;
}
public synchronized void log(Throwable e) {
boolean bAutoFlash = true;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(stream, bAutoFlash);
e.printStackTrace(writer);
if(stream.size() != 0)
log(DEBUG,stream.toString());
else
log(DEBUG,e.getClass().getName());
}
public synchronized void log(int level, String data) {
if(level < this.level)
return;
if(logPath == null) {
return;
}
else {
Calendar date = Calendar.getInstance();
String prefix = date.getTime().toString();
try {
PrintWriter ostream = new PrintWriter(getFileOutputStream());
ostream.write("["+prefix+"] " + data+"\r\n");
ostream.close();
}
catch(IOException e)
{}
}
}
public synchronized void setLogLevel(int level) {
this.level = level;
}
public int getLogLevel() {
return level;
}
/**
* Creates the logfilename based in the path provided and the date
*
* @return the logfilename
*/
public String getLogFileName() {
File file = null;
Calendar date = Calendar.getInstance();
String str = new Integer(date.get(Calendar.DAY_OF_MONTH)).toString();
int month = date.get(Calendar.MONTH) +1;
if( month < 10)
str += "0";
str += new Integer(month).toString();
str += new Integer(date.get(Calendar.YEAR)).toString();
str += ".log";
file = new File(logPath,str);
return file.getAbsolutePath();
}
private OutputStream getFileOutputStream() throws IOException {
return (OutputStream) new FileOutputStream(getLogFileName(),true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -