1. adjust log record time

2. add logger output flag: OUTPUT_WITH_PID

Signed-off-by: pengzhile <pengzhile@gmail.com>
This commit is contained in:
pengzhile 2022-02-22 15:43:49 +08:00
parent 17a4b412dd
commit a9df6b6634
4 changed files with 20 additions and 7 deletions

View File

@ -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

View File

@ -6,7 +6,7 @@
<groupId>com.ja-netfilter</groupId>
<artifactId>ja-netfilter</artifactId>
<version>2.3.1</version>
<version>2.3.2</version>
<name>ja-netfilter</name>
<description>A javaagent framework</description>

View File

@ -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;

View File

@ -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();