From d0ea067870779901c0db2624d1c5c7e430c9903b Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 26 Oct 2022 17:19:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Conflicts: # src/main/java/com/sunyard/log/SydLogFactory.java # src/main/java/com/sunyard/log/SydLogInfo.java # src/main/java/com/sunyard/log/SydLogger.java # src/main/java/racal/sunyard/main/SydApi4j.java --- .../java/com/sunyard/log/ILogFactory.java | 5 + src/main/java/com/sunyard/log/ILogger.java | 11 ++ .../java/com/sunyard/log/SydLogFactory.java | 118 ++++++++++++++++++ src/main/java/com/sunyard/log/SydLogInfo.java | 44 +++++++ src/main/java/com/sunyard/log/SydLogger.java | 42 +++++++ .../java/racal/sunyard/main/SydApi4j.java | 25 ++-- 6 files changed, 238 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/sunyard/log/ILogFactory.java create mode 100644 src/main/java/com/sunyard/log/ILogger.java create mode 100644 src/main/java/com/sunyard/log/SydLogFactory.java create mode 100644 src/main/java/com/sunyard/log/SydLogInfo.java create mode 100644 src/main/java/com/sunyard/log/SydLogger.java diff --git a/src/main/java/com/sunyard/log/ILogFactory.java b/src/main/java/com/sunyard/log/ILogFactory.java new file mode 100644 index 0000000..3f356bc --- /dev/null +++ b/src/main/java/com/sunyard/log/ILogFactory.java @@ -0,0 +1,5 @@ +package com.sunyard.log; + +public interface ILogFactory { + ILogger getLogger(Class cls); +} diff --git a/src/main/java/com/sunyard/log/ILogger.java b/src/main/java/com/sunyard/log/ILogger.java new file mode 100644 index 0000000..5141250 --- /dev/null +++ b/src/main/java/com/sunyard/log/ILogger.java @@ -0,0 +1,11 @@ +package com.sunyard.log; + +public interface ILogger { + + public int getCurrentLogLevel(); + + public void error(String msg, Object ...params); + public void info(String msg, Object ...params); + public void debug(String msg, Object ...params); + +} diff --git a/src/main/java/com/sunyard/log/SydLogFactory.java b/src/main/java/com/sunyard/log/SydLogFactory.java new file mode 100644 index 0000000..44914eb --- /dev/null +++ b/src/main/java/com/sunyard/log/SydLogFactory.java @@ -0,0 +1,118 @@ +package com.sunyard.log; +import java.io.File; +import java.io.FileOutputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +public class SydLogFactory implements ILogFactory { + // 日期格式 + private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + + // 日志目录 + public static String pcLogPath = "./sydapi-logs/"; + + // 日志名 + public static final String logName = "sydapi-log"; + + // 日志级别 + public static int iLogLevel = 0; + + // 日志队列 + private static ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); + + // 调试标记 + public static boolean debug = false; + + // 后台打印进程 + private static Executor executor = Executors.newSingleThreadExecutor(); + + static { + executor.execute(new Runnable() { + @Override + public void run() { + String logFileName = ""; + FileOutputStream logFos = null; + + while (true) { + try { + + if (queue.isEmpty()) { + // 释放 CPU + Thread.sleep(500); + continue; + } + + // 有日志 + // 确认日期 + String mayLogFileName = String.format("%s_%s.log", logName, format.format( new Date())); + if ( ! logFileName.equals( mayLogFileName ) ) { + logFileName = mayLogFileName; + if ( null != logFos ) { + logFos.flush(); + logFos.close(); + } + File folder = new File( pcLogPath ); + if ( ! folder.exists() ) { + folder.mkdirs(); + } + + File f = new File( pcLogPath, logFileName ); + if ( ! f.exists() ) { + f.createNewFile(); + } + logFos = new FileOutputStream( f , true); + } + + while (!queue.isEmpty()) { + SydLogInfo info = queue.poll(); + logFos.write(String.format("[%d:%s <%s-%d>] %s\r\n", + info.getLevel(), + format.format( new Date(info.getDatatime()) ), + info.getTname(), + info.getTid(), + info.getMsg() + ).getBytes() + ); + } + logFos.flush(); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + closeFileOutputStream(logFos); + logFileName = ""; + } + } + + } + }); + } + + + private static void closeFileOutputStream(FileOutputStream fos){ + if ( null != fos ) { + try { + fos.flush(); + }catch (Exception e) { + //pass + } + + try { + fos.close(); + }catch (Exception e) { + //pass + } + } + } + + private SydLogFactory() { + } + + @Override + public ILogger getLogger(Class cls) { + return new SydLogger(queue); + } +} diff --git a/src/main/java/com/sunyard/log/SydLogInfo.java b/src/main/java/com/sunyard/log/SydLogInfo.java new file mode 100644 index 0000000..52ec76b --- /dev/null +++ b/src/main/java/com/sunyard/log/SydLogInfo.java @@ -0,0 +1,44 @@ +package com.sunyard.log; + + +public class SydLogInfo { + private int level; + private long datatime; + private String tname; + private long tid; + private String msg; + private Object[] objects; + + public SydLogInfo(int level, String msg, Object[] objects) { + this.level = level; + this.msg = msg; + this.objects = objects; + this.datatime = System.currentTimeMillis(); + this.tname = Thread.currentThread().getName(); + this.tid = Thread.currentThread().getId(); + } + + public int getLevel() { + return level; + } + + public long getDatatime() { + return datatime; + } + + public String getTname() { + return tname; + } + + public long getTid() { + return tid; + } + + public String getMsg() { + return msg; + } + + public Object[] getObjects() { + return objects; + } +} diff --git a/src/main/java/com/sunyard/log/SydLogger.java b/src/main/java/com/sunyard/log/SydLogger.java new file mode 100644 index 0000000..6d7d63b --- /dev/null +++ b/src/main/java/com/sunyard/log/SydLogger.java @@ -0,0 +1,42 @@ +package com.sunyard.log; + +import java.util.concurrent.ConcurrentLinkedQueue; + +public class SydLogger implements ILogger{ + + private ConcurrentLinkedQueue queue; + + SydLogger(ConcurrentLinkedQueue queue) { + this.queue = queue; + } + + public void debug(String msg, Object... params) { + if (SydLogFactory.iLogLevel == 3) { + this.queue.add(new SydLogInfo(3, msg, params)); + } else { + return; + } + } + + public void info(String msg, Object... params) { + if (SydLogFactory.iLogLevel >= 2) { + this.queue.add(new SydLogInfo(2, msg, params)); + } else { + return; + } + } + + public void error(String msg, Object... params) { + if (SydLogFactory.iLogLevel >= 1) { + this.queue.add(new SydLogInfo(1, msg, params)); + } else { + return; + } + } + + @Override + public int getCurrentLogLevel() { + return SydLogFactory.iLogLevel; + } + +} diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index f0fc0c3..a44779b 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -8,6 +8,8 @@ import com.sunyard.SydApiException; import com.sunyard.cert.X509; import com.sunyard.constant.CertUsage; import com.sunyard.entity.Struct; +import com.sunyard.log.ILogFactory; +import com.sunyard.log.ILogger; import com.sunyard.proto.Packet; import com.sunyard.proto.PacketSection; import com.sunyard.proto.Util; @@ -76,11 +78,16 @@ public class SydApi4j implements SydApi { } } - private static void delta() { - long m = System.currentTimeMillis(); - System.out.print("delta \t"); - System.out.println(m - sTime); - sTime = m; + private static ILogFactory logFactory = null; + private static ILogger logger = null; + + public static ILogFactory getLogFactory() { + return logFactory; + } + + public static void setLogFactory(ILogFactory logFactory) { + SydApi4j.logFactory = logFactory; + SydApi4j.logger = logFactory.getLogger( SydApi4j.class ); } @@ -1119,14 +1126,18 @@ public class SydApi4j implements SydApi { if (debug) { System.out.println(i); } - + if ( null != SydApi4j.logger ) { + SydApi4j.logger.debug( "{}", i ); + } } private static void print(Object i) { if (debug) { System.out.print(i); } - + if ( null != SydApi4j.logger ) { + SydApi4j.logger.debug("{}", i ); + } } //gc回收