From 79c0a318a50a2c53351cb3e1cc4791657c84851f Mon Sep 17 00:00:00 2001 From: cheney Date: Fri, 2 Sep 2022 16:10:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sunyard/sge/log/LogInfo.java | 44 +++++++ .../com/sunyard/sge/log/SydLogFactory.java | 117 ++++++++++++++++++ .../java/com/sunyard/sge/log/SydLogger.java | 21 ++++ src/test/java/test/LogTest.java | 40 +++++- 4 files changed, 218 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/sunyard/sge/log/LogInfo.java create mode 100644 src/main/java/com/sunyard/sge/log/SydLogger.java diff --git a/src/main/java/com/sunyard/sge/log/LogInfo.java b/src/main/java/com/sunyard/sge/log/LogInfo.java new file mode 100644 index 0000000..f99b951 --- /dev/null +++ b/src/main/java/com/sunyard/sge/log/LogInfo.java @@ -0,0 +1,44 @@ +package com.sunyard.sge.log; + + +public class LogInfo { + private int level; + private long datatime; + private String tname; + private long tid; + private String msg; + private Object[] objects; + + public LogInfo(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/sge/log/SydLogFactory.java b/src/main/java/com/sunyard/sge/log/SydLogFactory.java index 6aab66e..f0173ec 100644 --- a/src/main/java/com/sunyard/sge/log/SydLogFactory.java +++ b/src/main/java/com/sunyard/sge/log/SydLogFactory.java @@ -1,4 +1,121 @@ package com.sunyard.sge.log; +import com.sunyard.sge.database.Consts; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; +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 { + // 日期格式 + private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + + // 金交所定制日志 + public static final String logName = "sge-hsm-sydapi-log"; + + // 日志级别 + public static int iLogLevel = Consts.LogLevelOFF; + + // 日志目录 + public static String pcLogPath = "./sydapi-logs/"; + + // 日志队列 + 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()) { + LogInfo 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(StandardCharsets.UTF_8) + ); + } + 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() { + } + + public static SydLogger getLogger() { + return new SydLogger(queue); + } } diff --git a/src/main/java/com/sunyard/sge/log/SydLogger.java b/src/main/java/com/sunyard/sge/log/SydLogger.java new file mode 100644 index 0000000..41924e7 --- /dev/null +++ b/src/main/java/com/sunyard/sge/log/SydLogger.java @@ -0,0 +1,21 @@ +package com.sunyard.sge.log; + +import java.util.concurrent.ConcurrentLinkedQueue; + +public class SydLogger { + + private ConcurrentLinkedQueue queue; + SydLogger(ConcurrentLinkedQueue queue) { + this.queue = queue; + } + + public void debug(String msg, Object... params){ + this.queue.add( new LogInfo(3, msg, params) ); + } + public void info(String msg, Object... params){ + this.queue.add( new LogInfo(2, msg, params) ); + } + public void error(String msg, Object... params){ + this.queue.add( new LogInfo(1, msg, params) ); + } +} diff --git a/src/test/java/test/LogTest.java b/src/test/java/test/LogTest.java index fc65240..7c814c8 100644 --- a/src/test/java/test/LogTest.java +++ b/src/test/java/test/LogTest.java @@ -1,14 +1,46 @@ package test; -import com.sunyard.sge.database.SydApi; -import com.sunyard.sge.database.SydApi4Database; import com.sunyard.sge.log.LogFactory; +import com.sunyard.sge.log.SydLogFactory; +import com.sunyard.sge.log.SydLogger; import org.apache.logging.log4j.Logger; - -import java.util.Arrays; +import org.junit.Test; public class LogTest { + + @Test + public void case1() throws InterruptedException { + int max = 10000; + int nThread = 10; + Thread[] threads = new Thread[nThread]; + for (int i = 0; i < nThread; i++) { + threads[i] = new Thread(new Runnable() { + @Override + public void run() { + SydLogger logger = SydLogFactory.getLogger(); + for (int i = 0; i < max; i++) { + logger.debug("tttttttttttttttt"); + } + } + }); + threads[i].start(); + } + + for (Thread t : threads) { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + Thread.sleep(10000 ); + System.out.println("over"); + + } + + public static void main(String[] args) { Logger log = LogFactory.createLogger("./test-log/", 3); log.error("error");