-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLoggingExample.java
More file actions
65 lines (52 loc) · 2.48 KB
/
FileLoggingExample.java
File metadata and controls
65 lines (52 loc) · 2.48 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
65
package src.examples;
import java.io.File;
import dev.nebalus.library.jlogger.Logger;
import dev.nebalus.library.jlogger.LogLevel;
import dev.nebalus.library.jlogger.handler.SyslogHandler;
import dev.nebalus.library.jlogger.handler.FileStreamHandler;
import dev.nebalus.library.jlogger.formatter.LineFormatter;
import dev.nebalus.library.jlogger.formatter.ColorLineFormatter;
import dev.nebalus.library.jlogger.formatter.colorscheme.DefaultColorScheme;
/**
* JLogger example demonstrating file logging and combining multiple handlers.
*
* Run with: java -cp "target/classes:examples" FileLoggingExample
*/
public class FileLoggingExample {
/**
* Runs the file logging example demonstrating multiple handlers.
*
* @param args command line arguments (not used)
*/
public static void main(String[] args) {
// Create logger
Logger logger = new Logger("FileDemo");
// --- Console Handler (colored, shows INFO and above) ---
SyslogHandler consoleHandler = new SyslogHandler(LogLevel.INFO, true); // bubble=true
consoleHandler.setFormatter(new ColorLineFormatter(
new DefaultColorScheme(),
null,
"HH:mm:ss"));
// --- File Handler (plain text, shows DEBUG and above) ---
File logFile = new File("application.log");
FileStreamHandler fileHandler = new FileStreamHandler(logFile, LogLevel.DEBUG, false);
fileHandler.setFormatter(new LineFormatter());
// Add handlers (order matters for bubbling)
logger.pushHandler(fileHandler); // First: write to file
logger.pushHandler(consoleHandler); // Second: write to console
System.out.println("Logging to console (INFO+) and file (DEBUG+)...\n");
// These will appear in both console and file
logger.error("Error: Database connection failed!");
logger.warning("Warning: Retrying in 5 seconds...");
logger.info("Connected to database successfully");
// These will only appear in the file (below INFO threshold for console)
logger.debug("Query executed in 15ms");
logger.debug("Cache hit ratio: 78%%");
// Log with class context
logger.info(FileLoggingExample.class, "Application started");
// Clean up - flushes file buffer
logger.close();
System.out.println("\n✓ File logging example completed!");
System.out.println("Check 'application.log' for all logged messages including DEBUG level.");
}
}