DhaverdLogs-2.0/Readme.md
2024-03-06 15:52:55 +08:00

1.9 KiB

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;

Examples:

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
// ...