Skip to content

Commit

Permalink
2022.5.3-Client
Browse files Browse the repository at this point in the history
  • Loading branch information
OptiJava committed May 27, 2022
1 parent 20ec241 commit 0bf7fc3
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 34 deletions.
Binary file modified .gradle/7.4/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.4/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.4/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.4/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.4/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
Binary file modified .gradle/workspace-id.txt.lock
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ _截止目前,Optilog代码总量2247行!_

**Optilog的优势**

1.性能高:初始化日志+输出一条日志(输出到屏幕+输出到文件+通过socket输出到服务端) 最多只需要194毫秒(后续还会再优化)。
1.性能高:初始化日志+输出一条日志(输出到屏幕+输出到文件+通过socket输出到服务端) 最多只需要162毫秒(后续还会再优化),但在你的你不用这个log实例之后要调用log.shutdown()

2.占位符方便:Optilog支持无限个占位符,并且可以重复使用(#1 #1输出两次第一个占位符)

Expand Down
Binary file modified build/classes/java/main/com/optilog/log/Log.class
Binary file not shown.
Binary file modified build/classes/java/main/com/optilog/log/LogInit.class
Binary file not shown.
Binary file modified build/classes/java/main/com/optilog/log/Optilog.class
Binary file not shown.
Binary file modified build/classes/java/main/com/optilog/log/Send.class
Binary file not shown.
Binary file modified build/classes/java/test/com/optilog/PerformanceTester.class
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Binary file modified build/tmp/compileTestJava/previous-compilation-data.bin
Binary file not shown.
4 changes: 4 additions & 0 deletions src/main/java/com/optilog/log/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,8 @@ static Optilog reInitLog(String pathOfSettingFile) {
void setServerFatal(boolean serverFatal);

void getAllField(Object instance);

void shutdown();

void shutdownNow();
}
2 changes: 2 additions & 0 deletions src/main/java/com/optilog/log/LogInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.optilog.util.exception.OptilogException;

import java.io.IOException;
import java.util.concurrent.Executors;

public class LogInit {
@OnlyInInit
public static void initLog(String settingFilePath, Optilog instance) {
instance.logThread = Executors.newCachedThreadPool();
if (settingFilePath.isBlank()) {
instance.consoleFileMasterCaution = false;
instance.allSetting = new SettingFiles();
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/com/optilog/log/Optilog.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,36 @@

import java.lang.reflect.Field;
import java.net.DatagramSocket;
import java.util.concurrent.ExecutorService;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Optilog implements Log {
public DatagramSocket socket;
public boolean consoleFileMasterCaution = true;
public boolean alreadyInit;
public volatile ExecutorService logThread;

public SettingFiles allSetting;
public volatile DatagramSocket socket;
public volatile boolean consoleFileMasterCaution = true;
public volatile boolean alreadyInit;

public String settingFilePath = "";
public volatile SettingFiles allSetting;

public String info = "";
public String error = "";
public String warn = "";
public String debug = "";
public String fatal = "";
public volatile String settingFilePath = "";

public volatile String info = "";
public volatile String error = "";
public volatile String warn = "";
public volatile String debug = "";
public volatile String fatal = "";

@Override
public void shutdown() {
this.logThread.shutdown();
}

@Override
public void shutdownNow() {
this.logThread.shutdownNow();
}

@Override
public void info() {
Expand Down
93 changes: 71 additions & 22 deletions src/main/java/com/optilog/log/Send.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,84 @@ void loggerPrint(String level, String message, Optilog instance) {

@OnlyInLog
void loggerConsole(String level, String message, Optilog instance) {
synchronized (Send.INSTANCE) {
try {
if (instance.consoleFileMasterCaution && Level.INFO.getName().equals(level) && !instance.info.isBlank()) {
Files.writeString(Path.of(instance.info), Files.readString(Path.of(instance.info), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
}
if (instance.consoleFileMasterCaution && Level.ERROR.getName().equals(level) && !instance.error.isBlank()) {
Files.writeString(Path.of(instance.error), Files.readString(Path.of(instance.error), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
}
if (instance.consoleFileMasterCaution && Level.DEBUG.getName().equals(level) && !instance.debug.isBlank()) {
Files.writeString(Path.of(instance.debug), Files.readString(Path.of(instance.debug), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
}
if (instance.consoleFileMasterCaution && Level.WARN.getName().equals(level) && !instance.warn.isBlank()) {
Files.writeString(Path.of(instance.warn), Files.readString(Path.of(instance.warn), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
}
if (instance.consoleFileMasterCaution && Level.FATAL.getName().equals(level) && !instance.fatal.isBlank()) {
Files.writeString(Path.of(instance.fatal), Files.readString(Path.of(instance.fatal), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
}
} catch (RuntimeException | IOException e) {
instance.consoleFileMasterCaution = false;
instance.error("Optilog Note:Java throws Exception when log is output", e);
try {
if (instance.consoleFileMasterCaution && Level.INFO.getName().equals(level) && !instance.info.isBlank()) {
String s = Packing.packMessage(message, level, instance);
instance.logThread.submit(() -> {
try {
synchronized (Send.INSTANCE) {
Files.writeString(Path.of(instance.info), Files.readString(Path.of(instance.info), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
}
} catch (IOException e) {
instance.consoleFileMasterCaution = false;
instance.error("Optilog Note:Java throws Exception when log is output", e);
}
});
}
if (instance.consoleFileMasterCaution && Level.ERROR.getName().equals(level) && !instance.error.isBlank()) {
String s = Packing.packMessage(message, level, instance);
instance.logThread.submit(() -> {
try {
synchronized (Send.INSTANCE) {
Files.writeString(Path.of(instance.error), Files.readString(Path.of(instance.error), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
}
} catch (IOException e) {
instance.consoleFileMasterCaution = false;
instance.error("Optilog Note:Java throws Exception when log is output", e);
}
});
}
if (instance.consoleFileMasterCaution && Level.DEBUG.getName().equals(level) && !instance.debug.isBlank()) {
String s = Packing.packMessage(message, level, instance);
instance.logThread.submit(() -> {
try {
synchronized (Send.INSTANCE) {
Files.writeString(Path.of(instance.debug), Files.readString(Path.of(instance.debug), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
}
} catch (IOException e) {
instance.consoleFileMasterCaution = false;
instance.error("Optilog Note:Java throws Exception when log is output", e);
}
});
}
if (instance.consoleFileMasterCaution && Level.WARN.getName().equals(level) && !instance.warn.isBlank()) {
String s = Packing.packMessage(message, level, instance);
instance.logThread.submit(() -> {
try {
synchronized (Send.INSTANCE) {
Files.writeString(Path.of(instance.warn), Files.readString(Path.of(instance.warn), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
}
} catch (IOException e) {
instance.consoleFileMasterCaution = false;
instance.error("Optilog Note:Java throws Exception when log is output", e);
}
});
}
if (instance.consoleFileMasterCaution && Level.FATAL.getName().equals(level) && !instance.fatal.isBlank()) {
String s = Packing.packMessage(message, level, instance);
instance.logThread.submit(() -> {
try {
synchronized (Send.INSTANCE) {
Files.writeString(Path.of(instance.fatal), Files.readString(Path.of(instance.fatal), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
}
} catch (IOException e) {
instance.consoleFileMasterCaution = false;
instance.error("Optilog Note:Java throws Exception when log is output", e);
}
});
}
} catch (RuntimeException e) {
instance.consoleFileMasterCaution = false;
instance.error("Optilog Note:Java throws Exception when log is output", e);
}
}

@OnlyInLog
void loggerToServer(String level, String message, Optilog instance) {
void loggerToServer(final String level, String message, final Optilog instance) {
synchronized (Send.INSTANCE) {
message = Packing.packMessage(message, level, instance);
Client.send(message + level, instance);
String finalMessage = message;
instance.logThread.submit(() -> Client.send(finalMessage + level, instance));
}
}
}
8 changes: 7 additions & 1 deletion src/test/java/com/optilog/PerformanceTester.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.optilog;

import com.optilog.log.Log;

public class PerformanceTester {
public static void main(String[] args) {

long l = System.currentTimeMillis();
Log log = Log.initLog("%prop -cp /Settings.properties");
log.info("Hello World!");
log.shutdown();
System.out.println(System.currentTimeMillis() - l);
}
}
1 change: 1 addition & 0 deletions src/test/resources/logs/2022-05-27 12-40-49Log.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2022-05-27|12:40:49(83))][com.optilog.PerformanceTester main(PerformanceTester.java:9)/main] info:Hello World!
1 change: 1 addition & 0 deletions src/test/resources/logs/2022-05-27 12-45-22Log.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2022-05-27|12:45:22(87))][com.optilog.PerformanceTester main(PerformanceTester.java:9)/main] info:Hello World!

0 comments on commit 0bf7fc3

Please sign in to comment.