Slightly improved safety; Added Readme
This commit is contained in:
parent
628bfa3a46
commit
1c9047e298
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -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
|
||||||
|
// ...
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,14 @@ public class DLogger {
|
||||||
private String fileName;
|
private String fileName;
|
||||||
private boolean autoGenerateFileName;
|
private boolean autoGenerateFileName;
|
||||||
private static String logDir = "";
|
private static String logDir = "";
|
||||||
|
private String[] unacceptableChars = {"\\", "/", ":", "*", "?", "\"", "<", ">", "|"};
|
||||||
|
|
||||||
private boolean customLogDir;
|
private boolean customLogDir;
|
||||||
|
|
||||||
public DLogger(String fileName, boolean autoGenerateFileName) {
|
public DLogger(boolean autoGenerateFileName, String ...fileName) {
|
||||||
this.fileName = fileName;
|
if (fileName.length != 0){
|
||||||
|
this.fileName = replaceUnacceptableChar(fileName[0]);
|
||||||
|
}
|
||||||
this.autoGenerateFileName = autoGenerateFileName;
|
this.autoGenerateFileName = autoGenerateFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +30,13 @@ public class DLogger {
|
||||||
this.customLogDir = true;
|
this.customLogDir = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String replaceUnacceptableChar(String str){
|
||||||
|
for (String character : unacceptableChars){
|
||||||
|
str = str.replace(character, "_");
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
private String getCurrentTime(){
|
private String getCurrentTime(){
|
||||||
String time;
|
String time;
|
||||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
Loading…
Reference in New Issue