📄 writelog.java
字号:
package com.wayout.wayoutsp.publics;
import java.io.*;
import java.util.*;
/**
*
* <p>Title: 写日志</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: wayout </p>
* @author hecong
* @version 1.0
*/
public class WriteLog {
private Calendar now;
private FileWriter fw;
private String pathStr;
private String nameStr;
private static int debugLevel = 0;
/**
* 文件是否打开
*/
private boolean fileOpen = false;
/**
* 构造方法.
*
* @param logPath 日志路径
* @throws IOException
*/
public WriteLog(String logPath) throws IOException {
pathStr = logPath;
File p = new File(pathStr);
if (!p.exists())
p.mkdirs();
// Get current time parameter
now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH)+1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
mm = (1==mm.length()) ? ("0"+mm) : mm;
dd = (1==dd.length()) ? ("0"+dd) : dd;
nameStr = yyyy + mm + dd;
// Make today's log file full name.
String fileName = pathStr + nameStr + ".log";
fw = new FileWriter(fileName,true);
fileOpen = true;
}
/**
* 构造方法.
*
* @param logPath 日志路径
* @param debug 调试等级
* @throws IOException
*/
public WriteLog(String logPath, int debug) throws IOException {
debugLevel = debug;
pathStr = logPath;
File p = new File(pathStr);
if (!p.exists())
p.mkdirs();
// Get current time parameter
now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH)+1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
mm = (1==mm.length()) ? ("0"+mm) : mm;
dd = (1==dd.length()) ? ("0"+dd) : dd;
nameStr = yyyy + mm + dd;
// Make today's log file full name.
String fileName = pathStr + nameStr + ".log";
fw = new FileWriter(fileName,true);
fileOpen = true;
}
/**
* 将数据包msg写入日志文件.
*
* @param note 说明串
* @param msg 数据包
*/
public synchronized void packDebug(String note, byte[] msg) {
writePackLog(note, msg);
}
/**
* 打印cmpp信息.
*
* @param msg 打印内容
*/
public synchronized void cmppPrint(String msg) {
System.out.println("Cmpp "+msg);
}
/**
* 写基本信息.
*
* @param msg 写内容
*/
public synchronized void info(String msg) {
writeToTodayLog("", msg);
}
/**
* 写基本信息.
*
* @param msg 写内容
* @param debug 调试等级
*/
public synchronized void info(String msg, int debug) {
if(debug<=debugLevel) {
writeToTodayLog("", msg);
}
}
/**
* 写警告信息.
*
* @param msg 写内容
*/
public synchronized void warning(String msg) {
writeToTodayLog("WARNING", msg);
}
public synchronized void warning(String msg, int debug) {
if(debug<=debugLevel) {
writeToTodayLog("WARNING", msg);
}
}
/**
* 写错误信息.
*
* @param msg 写内容
*/
public synchronized void error(String msg) {
writeToTodayLog("ERROR", msg);
}
public synchronized void error(String msg,int debug) {
if(debug<=debugLevel) {
writeToTodayLog("ERROR", msg);
}
}
/**
* 写致命错误信息.
*
* @param msg 写内容
*/
public synchronized void fatal(String msg) {
writeToTodayLog("FATAL", msg);
}
/**
* 写信息入分为今天的日志文件.
*
* @param flag 写肉类
* @param msg 写内容
*/
private void writeToTodayLog(String flag,String msg) {
try {
// Get current time parameter
now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH)+1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
String hh = String.valueOf(now.get(Calendar.HOUR_OF_DAY));
String ff = String.valueOf(now.get(Calendar.MINUTE));
String ss = String.valueOf(now.get(Calendar.SECOND));
mm = (1==mm.length()) ? ("0"+mm) : mm;
dd = (1==dd.length()) ? ("0"+dd) : dd;
String yyyymmdd = yyyy + mm + dd;
if ( ( ! yyyymmdd.equals(nameStr) )||(fileOpen==false) ) {
fwClose();
fileOpen = false;
nameStr = yyyymmdd;
String fileName = pathStr + nameStr + ".log";
fw = new FileWriter(fileName,true);
fileOpen = true;
}
String hhffss = hh + ":" + ff + ":" + ss;
if( flag != "" )
hhffss = flag + ": " + hhffss ;
fw.write("\r\n"+hhffss +" ");
fw.write(msg);
fw.flush() ;
} catch(Exception ioe) {
System.out.println("WriteLog: "+ioe);
fwClose();
fileOpen = false;
}
}
/**
* 写数据包入包日志文件.
*
* @param note 说明串
* @param pack 数据包
*/
private void writePackLog(String note,byte[] pack) {
FileWriter fp = null;
try {
// Get current time parameter
now = Calendar.getInstance();
String yyyy = String.valueOf(now.get(Calendar.YEAR));
String mm = String.valueOf(now.get(Calendar.MONTH)+1);
String dd = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
String hh = String.valueOf(now.get(Calendar.HOUR_OF_DAY));
String ff = String.valueOf(now.get(Calendar.MINUTE));
String ss = String.valueOf(now.get(Calendar.SECOND));
mm = (1==mm.length()) ? ("0"+mm) : mm;
dd = (1==dd.length()) ? ("0"+dd) : dd;
String yyyymmdd = yyyy + mm + dd;
String pnameStr = "pack"+yyyymmdd;
String fileName = pathStr + pnameStr + ".log";
fp = new FileWriter(fileName,true);
String hhffss = hh + ":" + ff + ":" + ss;
if( note != "" )
hhffss = note + " \t" + hhffss ;
fp.write("\n"+hhffss +"\n");
for( int i =0; i<= (pack.length/16) ; i++ ) {
for( int j =0; j<16 ; j++ ) {
fp.write(" ");
if( i*16+j < pack.length ) {
String sss = Integer.toHexString(pack[i*16+j]);
if ( sss.length()==1 )
sss = " "+sss;
else if ( sss.length() == 8 )
sss = sss.substring(6,8) ;
fp.write(sss);
//fp.write(Integer.toHexString(pack[i*8+j]) );
}
else
break;
}
fp.write("\n");
}
fp.write("\n");
fp.close() ;
} catch(IOException ioe) {
System.out.println("writePackLog: "+ioe);
try {
fp.close();
} catch (Exception e) {}
}
}
/**
* 关闭日志文件
*/
private void fwClose() {
try {
fw.close();
} catch (Exception e) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -