forked from UnlegitDqrk/unlegitlibrary
Logged stacktrace
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>me.finn.unlegitlibrary</groupId>
|
<groupId>me.finn.unlegitlibrary</groupId>
|
||||||
<artifactId>unlegitlibrary</artifactId>
|
<artifactId>unlegitlibrary</artifactId>
|
||||||
<version>1.5.2-Java1.8</version>
|
<version>1.5.3-Java1.8</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
@@ -10,6 +10,9 @@ package me.finn.unlegitlibrary.string.color;
|
|||||||
|
|
||||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ConsoleColor extends DefaultMethodsOverrider {
|
public class ConsoleColor extends DefaultMethodsOverrider {
|
||||||
// Reset
|
// Reset
|
||||||
public static final String RESET = "\033[0m"; // Text Reset
|
public static final String RESET = "\033[0m"; // Text Reset
|
||||||
|
@@ -3,12 +3,22 @@ package me.finn.unlegitlibrary.utils;
|
|||||||
import me.finn.unlegitlibrary.file.FileUtils;
|
import me.finn.unlegitlibrary.file.FileUtils;
|
||||||
import me.finn.unlegitlibrary.string.color.ConsoleColor;
|
import me.finn.unlegitlibrary.string.color.ConsoleColor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.awt.*;
|
||||||
import java.io.IOException;
|
import java.awt.datatransfer.Clipboard;
|
||||||
|
import java.awt.datatransfer.DataFlavor;
|
||||||
|
import java.awt.datatransfer.Transferable;
|
||||||
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.io.*;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.System;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger by shock9 Interactive
|
* Logger by shock9 Interactive
|
||||||
@@ -17,9 +27,16 @@ public final class Logger {
|
|||||||
private File logFolder;
|
private File logFolder;
|
||||||
private File latestLogFile;
|
private File latestLogFile;
|
||||||
|
|
||||||
private static boolean isInitialized = false;
|
private boolean isInitialized = false;
|
||||||
|
|
||||||
|
public Logger(File logFolder, boolean addShutdownHook) throws IOException, NoSuchFieldException, IllegalAccessException {
|
||||||
|
Field field = Charset.class.getDeclaredField("defaultCharset");
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(null, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
System.setProperty("client.encoding.override", "UTF-8");
|
||||||
|
System.setProperty("file.encoding", "UTF-8");
|
||||||
|
|
||||||
public Logger(File logFolder) throws IOException {
|
|
||||||
// Basic setup for log folder and latest log file
|
// Basic setup for log folder and latest log file
|
||||||
this.logFolder = logFolder;
|
this.logFolder = logFolder;
|
||||||
latestLogFile = new File(logFolder, "log-latest.txt");
|
latestLogFile = new File(logFolder, "log-latest.txt");
|
||||||
@@ -29,15 +46,17 @@ public final class Logger {
|
|||||||
if (latestLogFile.exists()) latestLogFile.delete();
|
if (latestLogFile.exists()) latestLogFile.delete();
|
||||||
latestLogFile.createNewFile();
|
latestLogFile.createNewFile();
|
||||||
|
|
||||||
isInitialized = true;
|
if (addShutdownHook) {
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
try {
|
||||||
|
shutdown();
|
||||||
|
} catch (IOException exception) {
|
||||||
|
exception("Failed to shutdown logger", exception);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
isInitialized = true;
|
||||||
try {
|
|
||||||
shutdown();
|
|
||||||
} catch (IOException exception) {
|
|
||||||
exception("Failed to shutdown logger", exception);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renaming latest log to current date and yeah
|
// Renaming latest log to current date and yeah
|
||||||
@@ -144,8 +163,16 @@ public final class Logger {
|
|||||||
|
|
||||||
String timeStamp = formatter.format(date);
|
String timeStamp = formatter.format(date);
|
||||||
|
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
PrintWriter pw = new PrintWriter(sw);
|
||||||
|
exception.printStackTrace(pw);
|
||||||
|
String stackTrace = sw.toString();
|
||||||
|
|
||||||
// Writing log
|
// Writing log
|
||||||
String log = timeStamp + " [EXCEPTION-INFO] " + infoLine + System.lineSeparator() + timeStamp + " [EXCEPTION-MESSAGE] " + exception.getMessage();
|
String log =
|
||||||
|
timeStamp + " [EXCEPTION-INFO] " + infoLine + System.lineSeparator() +
|
||||||
|
timeStamp + " [EXCEPTION-MESSAGE] " + exception.getMessage() + System.lineSeparator() +
|
||||||
|
timeStamp + " [EXCEPTION-STACKTRACE] " + stackTrace;
|
||||||
|
|
||||||
System.out.println(ConsoleColor.RED + log + ConsoleColor.RESET);
|
System.out.println(ConsoleColor.RED + log + ConsoleColor.RESET);
|
||||||
|
|
||||||
@@ -174,4 +201,4 @@ public final class Logger {
|
|||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user