最近在看 Bob 大叔的《代码整洁之道》,为了在实践中体会,写了一小段代码,功能是进行时间戳转换,第一版完成后根据书中的一些原则重构了下,然后再其中加入了日志记录模块,使用的是 java 自带的 java.util.logging, 主要是看下日志怎么记录,这个过程中也考虑并学习了日志应该记录什么信息,其中还有很多不完善的地方,完整代码如下。
// Main.java
package trans;
import java.util.Scanner;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args){
Timestamp ts = new Timestamp();
ts.initLogger();
Scanner scanner = new Scanner(System.in);
final Logger logger = ts.logger;
while(true) {
logger.info("Start Choice!");
System.out.println("******************************");
System.out.println("Chose from:\n" +
"0. Exit\n" +
"1. Date transform to timestamp\n" +
"2. trans.Timestamp transform to date");
String choice = scanner.nextLine();
switch (choice) {
case "1":
System.out.print("Enter Date(format 2020-9-26 11:10:24): ");
ts.transDate(scanner, logger);
break;
case "2":
System.out.print("Enter trans.Timestamp: ");
ts.transStamp(scanner, logger);
break;
case "0":
logger.info("Program Exit!");
return;
default:
logger.info("Invalid choice, choose again!");
break;
}
System.out.print("\n\n");
}
}
}
// Timestamp.java
package trans;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Timestamp {
String date;
long timestamp;
Logger logger;
void initLogger(){
MyLogger logger = new MyLogger("TransDateTimestamp");
logger.setLoggerLevel(Level.INFO);
logger.setFileHandler("./Trans" +
"DateTimstamp.log");
this.logger = logger.logger;
}
void transDate(Scanner scanner, Logger logger){
try{
this.date = scanner.nextLine();
this.timestamp = this.date2stamp(this.date);
logger.info("Trans successfully!");
System.out.print("\033[34;1m" + "Date: " + this.date + "\ntrans.Timestamp: "
+ this.timestamp + "\033[0m");
} catch (ParseException e) {
logger.warning("Input Error, Date Format incorrect!");
}
}
long date2stamp(String date) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
Date dateTime = dateFormat.parse(date);
return dateTime.getTime();
}
void transStamp(Scanner scanner, Logger logger){
try {
this.timestamp = Long.parseLong(scanner.nextLine());
this.date = this.stamp2date(this.timestamp);
logger.info("Trans successfully!");
System.out.print("\033[34;1m" + "timestamp: " + this.timestamp +"\ndate: "
+ this.date + "\033[0m");
}catch(NumberFormatException e){
logger.warning("Input Error! Timestamp format incorrect!");
}
}
String stamp2date(long timestamp){
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
Date datetime = new Date(timestamp);
return dateFormat.format(datetime);
}
}
// MyLogger.java
package trans;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.Level;
public class MyLogger {
Logger logger;
MyLogger(String loggerName){
logger = Logger.getLogger(loggerName);
}
public void setLoggerLevel(Level level){
logger.setLevel(level);
}
public void setFileHandler(String filePath){
try{
FileHandler fileHandler = new FileHandler(filePath);
logger.addHandler(fileHandler);
}catch(IOException e){
e.printStackTrace();
}
}
}