From a9df6b663460b6f70ac0b2f10713e943d2102c84 Mon Sep 17 00:00:00 2001 From: pengzhile Date: Tue, 22 Feb 2022 15:43:49 +0800 Subject: [PATCH] 1. adjust log record time 2. add logger output flag: OUTPUT_WITH_PID Signed-off-by: pengzhile --- README.md | 5 +++-- pom.xml | 2 +- .../java/com/janetfilter/core/Launcher.java | 2 +- .../janetfilter/core/commons/DebugInfo.java | 18 +++++++++++++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2bc1f15..7780487 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ja-netfilter v2.3.1 +# ja-netfilter v2.3.2 ### A javaagent framework @@ -56,7 +56,8 @@ EQUAL,somedomain * the `ja-netfilter` will output debugging information to the `console` by default * add environment variable `JANF_OUTPUT=value` and start to change output medium * or add system property `-Djanf.output=value` to change output medium -* output medium value: [`NONE=0`, `CONSOLE=1`, `FILE=2`, `CONSOLE+FILE=3`] +* output medium value: [`NONE=0`, `CONSOLE=1`, `FILE=2`, `CONSOLE+FILE=3`, `WITH_PID=4`] +* eg: `console` + `file` + `pid file name` = 1 + 2 + 4 = 7, so the `-Djanf.output=7` ## Plugin system diff --git a/pom.xml b/pom.xml index bb0942b..e50b503 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.ja-netfilter ja-netfilter - 2.3.1 + 2.3.2 ja-netfilter A javaagent framework diff --git a/src/main/java/com/janetfilter/core/Launcher.java b/src/main/java/com/janetfilter/core/Launcher.java index d6e013d..3db4bc5 100644 --- a/src/main/java/com/janetfilter/core/Launcher.java +++ b/src/main/java/com/janetfilter/core/Launcher.java @@ -12,7 +12,7 @@ import java.util.jar.JarFile; public class Launcher { public static final String ATTACH_ARG = "--attach"; - public static final String VERSION = "v2.3.1"; + public static final String VERSION = "v2.3.2"; private static boolean loaded = false; diff --git a/src/main/java/com/janetfilter/core/commons/DebugInfo.java b/src/main/java/com/janetfilter/core/commons/DebugInfo.java index c7c9ea9..3d161c2 100644 --- a/src/main/java/com/janetfilter/core/commons/DebugInfo.java +++ b/src/main/java/com/janetfilter/core/commons/DebugInfo.java @@ -8,12 +8,14 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; +import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class DebugInfo { private static final long OUTPUT_CONSOLE = 0x1L; private static final long OUTPUT_FILE = 0x2L; + private static final long OUTPUT_WITH_PID = 0x4L; private static final ExecutorService CONSOLE_EXECUTOR = Executors.newSingleThreadExecutor(); private static final ExecutorService FILE_EXECUTOR = Executors.newSingleThreadExecutor(); @@ -145,6 +147,7 @@ public class DebugInfo { private final Throwable exception; private final Throwable stackException; private final String threadName; + private final Date dateTime; private PrintStream ps; @@ -155,6 +158,7 @@ public class DebugInfo { this.exception = exception; this.stackException = new Throwable(); this.threadName = Thread.currentThread().getName(); + this.dateTime = new Date(); setPrintStream(null == exception ? System.out : System.err); } @@ -193,6 +197,14 @@ public class DebugInfo { this.ps = ps; } + protected String getPID() { + return pid; + } + + public Date getDateTime() { + return dateTime; + } + @Override public void run() { int line = 0; @@ -207,7 +219,7 @@ public class DebugInfo { } } - String outContent = String.format(LOG_TEMPLATE, DateUtils.formatDateTimeMicro(), level, threadName, pid, caller, line, content); + String outContent = String.format(LOG_TEMPLATE, DateUtils.formatDateTimeMicro(dateTime), level, threadName, pid, caller, line, content); write(outContent, exception, getPrintStream()); } } @@ -223,9 +235,9 @@ public class DebugInfo { @Override public void run() { - File logFile = new File(logDir, String.format("%s.log", DateUtils.formatDate())); + String fileName = String.format("%s%s.log", DateUtils.formatDate(getDateTime()), 0 != (LOG_OUTPUT & OUTPUT_WITH_PID) ? "-" + getPID() : ""); - try (PrintStream ps = new PrintStream(new FileOutputStream(logFile, true))) { + try (PrintStream ps = new PrintStream(new FileOutputStream(new File(logDir, fileName), true))) { setPrintStream(ps); super.run();