📄 writelog.java
字号:
import java.io.*;
import java.util.*;
import javax.swing.*;
public class writeLog {
public static void writeLog(String CardID,String event){
try {
String logPath=logPathGet();
String logFile="log";
Calendar date=new GregorianCalendar();
int year=date.get(Calendar.YEAR);
int month=date.get(Calendar.MONTH)+1;
int data=date.get(Calendar.DAY_OF_MONTH);
int hour=date.get(Calendar.HOUR_OF_DAY);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
logFile=logPath+"\\"+logFile;
logFile+=year;
logFile+=(month<10?"0":"")+month;
logFile+=(data<10?"0":"")+data;
logFile+=".txt";
// 按日期每天生成一个日志文件
FileWriter fwl = new FileWriter(logFile,true);
PrintWriter outl = new PrintWriter(fwl);
outl.println("["+hour+":"+minute+":"+second+ "] " + CardID+" "+event);
outl.close();
fwl.close();
}catch (IOException ex) {
JOptionPane.showMessageDialog(null,"01文件读取错误!","文件读取错误",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void writeLog(String str) {
try{
String logPath=portPane.getLogPath();
String logFile="log";
Calendar date=new GregorianCalendar();
int year=date.get(Calendar.YEAR);
int month=date.get(Calendar.MONTH)+1;
int data=date.get(Calendar.DAY_OF_MONTH);
int hour=date.get(Calendar.HOUR_OF_DAY);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
logFile=logPath+"\\"+logFile;
logFile+=year;
logFile+=(month<10?"0":"")+month;
logFile+=(data<10?"0":"")+data;
logFile+=".txt";
// 按日期每天生成一个日志文件
FileWriter fwl = new FileWriter(logFile,true);
PrintWriter outl = new PrintWriter(fwl);
outl.println("["+hour+":"+minute+":"+second+ "] "+ str);
outl.close();
fwl.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"02文件读取错误!","文件读取错误",JOptionPane.ERROR_MESSAGE);
}
}
private static String logPathGet(){
String str=null;
try{
BufferedReader in=new BufferedReader(new FileReader("web\\logpath.txt"));
str=in.readLine();
}catch(Exception e){}
if(str==null) {
str="D:\\newbase\\ProxyServer\\Log";
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -