-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicLoggingExample.java
More file actions
64 lines (53 loc) · 2.22 KB
/
BasicLoggingExample.java
File metadata and controls
64 lines (53 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package src.examples;
import dev.nebalus.library.jlogger.Logger;
import dev.nebalus.library.jlogger.LogLevel;
import dev.nebalus.library.jlogger.handler.SyslogHandler;
import dev.nebalus.library.jlogger.formatter.LineFormatter;
/**
* Basic JLogger example demonstrating all log levels and message formatting.
*
* Run with: java -cp "target/classes:examples" BasicLoggingExample
*/
public class BasicLoggingExample {
/**
* Runs the basic logging example demonstrating all log levels.
*
* @param args command line arguments (not used)
*/
public static void main(String[] args) {
// Create a logger with a channel name
Logger logger = new Logger("MyApp");
// Add a console handler that shows all log levels (DEBUG has lowest priority)
SyslogHandler consoleHandler = new SyslogHandler(LogLevel.DEBUG, false);
consoleHandler.setFormatter(new LineFormatter());
logger.pushHandler(consoleHandler);
// Log at different severity levels
logger.emergency("System is unusable!");
logger.alert("Immediate action required!");
logger.fatal("Critical error occurred!");
logger.error("An error happened");
logger.warning("Something might be wrong");
logger.info("Informational message");
logger.log("Default log message");
logger.debug("Debug information");
// Message formatting with arguments
String username = "john_doe";
int itemCount = 42;
logger.info("User %s has %d items in cart", username, itemCount);
// Class-context logging (adds class name as label)
logger.info(BasicLoggingExample.class, "Processing started");
logger.warning(BasicLoggingExample.class, "Slow operation detected: %dms", 1500);
// Using custom log level
logger.log(LogLevel.WARNING, "Custom level log message");
// Exception logging
try {
throw new RuntimeException("Something went wrong!");
} catch (Exception e) {
logger.error(e);
logger.error(BasicLoggingExample.class, e);
}
// Clean up resources
logger.close();
System.out.println("\n✓ Basic logging example completed!");
}
}