diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..1875f08 --- /dev/null +++ b/Readme.md @@ -0,0 +1,75 @@ +# What it is + +This is simple logging library for Java, mostly written for myself. It can log what you want into file and console output. + +# Usage + +### Initialize + +There is 2 way to initialize the DLogger class. There is no particular difference between them. + +``` +DLogger logger = new DLogger(true); +// Log file name will be: 2024.03.06_14-37-12_log.log + +OR + +DLogger logger = new DLogger(false, "log.log"); +// Log file name will be: 2024.03.06_14-37-12_your_log_name.log +// Chars \ / : * ? " < > | will be replace with _ +``` + +### Using + +Now you can set up logging directory. But you don't have to, then the default directory will be the project root "./". + +``` +logger.setLogDir("/home/user/logs/"); +``` + +Next you can use logger. Logger accepts 4 parameters: + +- String context - a property added for convenience that allows you to manually indicate where a log line was called from; +- String text - log text; +- boolean addTime - should logger add current time to log line +- boolean addIndent - should logger add indent (22 spaces) to log line + +Example: + +``` +logger.log("Some context", "Some text", false false); +// Output +// [Some context] Some text + + +logger.log("MyClass", "Abracadabra", true, false); +// Output +// [2024-03-06 15:28:31] [MyClass] Abracadabra + + +logger.log("MyClass", "Abracadabra", false, true); +// Output +// [MyClass] Abracadabra + + +logger.log("MyClass", "Abracadabra", true, true); +// Output +// [2024-03-06 15:28:31] [MyClass] Abracadabra + + +// By the way you can use logger to log exeptions +... +catch(Exeption e){ + for (StackTraceElement trace : e.getStackTrace()){ + logger.log("MyClass Exeption", trace.toString(), true, false); + } +} +// Output +// [2024-03-06 15:28:31] [MyClass Exeption] NullPointerExeption +// [2024-03-06 15:28:31] [MyClass Exeption] Exeption trace line 1 +// [2024-03-06 15:28:31] [MyClass Exeption] Exeption trace line 2 +// ... +``` + + + \ No newline at end of file diff --git a/src/dhaverdLogs/DLogger.java b/src/dhaverdLogs/DLogger.java index 7c36de5..276f999 100644 --- a/src/dhaverdLogs/DLogger.java +++ b/src/dhaverdLogs/DLogger.java @@ -14,11 +14,14 @@ public class DLogger { private String fileName; private boolean autoGenerateFileName; private static String logDir = ""; + private String[] unacceptableChars = {"\\", "/", ":", "*", "?", "\"", "<", ">", "|"}; private boolean customLogDir; - public DLogger(String fileName, boolean autoGenerateFileName) { - this.fileName = fileName; + public DLogger(boolean autoGenerateFileName, String ...fileName) { + if (fileName.length != 0){ + this.fileName = replaceUnacceptableChar(fileName[0]); + } this.autoGenerateFileName = autoGenerateFileName; } @@ -27,6 +30,13 @@ public class DLogger { this.customLogDir = true; } + private String replaceUnacceptableChar(String str){ + for (String character : unacceptableChars){ + str = str.replace(character, "_"); + } + return str; + } + private String getCurrentTime(){ String time; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");