-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.DesignALoggerSystem.java
More file actions
300 lines (262 loc) · 10.1 KB
/
03.DesignALoggerSystem.java
File metadata and controls
300 lines (262 loc) · 10.1 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
/* ------------------------ ENUM for Log Levels ------------------------ */
enum Level {
DEBUG(10), INFO(20), WARNING(30), ERROR(40), CRITICAL(50);
private final int value;
Level(int v) { this.value = v; }
public int getValue() { return value; }
}
/* ------------------------ LogRecord Class ------------------------ */
class LogRecord {
String loggerName;
Level level;
String message;
Map<String, Object> metadata;
long timestamp;
long seq;
LogRecord(String name, Level lvl, String msg, Map<String, Object> meta) {
this.loggerName = name;
this.level = lvl;
this.message = msg;
this.metadata = meta != null ? meta : new HashMap<>();
this.timestamp = System.currentTimeMillis();
this.seq = System.identityHashCode(this);
}
}
/* ------------------------ Formatter ------------------------ */
class Formatter {
private final String fmt;
Formatter(String fmt) {
this.fmt = fmt;
}
String format(LogRecord record) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String ts = sdf.format(new Date(record.timestamp));
StringBuilder metaStr = new StringBuilder();
for (Map.Entry<String, Object> e : record.metadata.entrySet()) {
metaStr.append(e.getKey()).append("=").append(e.getValue()).append(" ");
}
return fmt
.replace("{asctime}", ts)
.replace("{level}", record.level.name())
.replace("{name}", record.loggerName)
.replace("{msg}", record.message)
.replace("{meta}", metaStr.toString().trim());
}
}
/* ------------------------ Handler Base Class ------------------------ */
abstract class Handler {
protected Level level;
protected Formatter formatter;
Handler(Level lvl, Formatter fmt) {
this.level = lvl;
this.formatter = fmt != null ? fmt : new Formatter("{asctime} [{level}] {name}: {msg} {meta}");
}
void handle(LogRecord record) {
if (record.level.getValue() >= level.getValue()) {
try {
emit(record);
} catch (Exception e) {
System.err.println("Handler error: " + e.getMessage());
}
}
}
abstract void emit(LogRecord record) throws Exception;
}
/* ------------------------ ConsoleHandler ------------------------ */
class ConsoleHandler extends Handler {
ConsoleHandler(Level lvl, Formatter fmt) {
super(lvl, fmt);
}
@Override
void emit(LogRecord record) {
System.out.println(formatter.format(record));
}
}
/* ------------------------ RotatingFileHandler ------------------------ */
class RotatingFileHandler extends Handler {
private final String filename;
private final long maxBytes;
private final int backupCount;
private BufferedWriter writer;
RotatingFileHandler(String filename, long maxBytes, int backupCount, Level lvl, Formatter fmt) throws IOException {
super(lvl, fmt);
this.filename = filename;
this.maxBytes = maxBytes;
this.backupCount = backupCount;
openFile();
}
private void openFile() throws IOException {
File file = new File(filename);
file.getParentFile().mkdirs();
this.writer = new BufferedWriter(new FileWriter(file, true));
}
private boolean shouldRotate() {
File f = new File(filename);
return f.exists() && f.length() >= maxBytes;
}
private void rotate() throws IOException {
writer.close();
for (int i = backupCount - 1; i >= 1; i--) {
File src = new File(filename + "." + i);
File dest = new File(filename + "." + (i + 1));
if (src.exists()) src.renameTo(dest);
}
File current = new File(filename);
if (current.exists()) current.renameTo(new File(filename + ".1"));
openFile();
}
@Override
void emit(LogRecord record) throws IOException {
writer.write(formatter.format(record));
writer.newLine();
writer.flush();
if (shouldRotate()) rotate();
}
}
/* ------------------------ AsyncDispatcher ------------------------ */
class AsyncDispatcher {
private final BlockingQueue<LogRecord> queue;
private final List<Handler> handlers;
private final int batchSize;
private final long flushIntervalMs;
private final String dropPolicy;
private final Thread worker;
private final AtomicBoolean running = new AtomicBoolean(false);
private int dropped = 0;
AsyncDispatcher(List<Handler> handlers, int queueSize, int batchSize, long flushIntervalMs, String dropPolicy) {
this.handlers = handlers;
this.queue = new ArrayBlockingQueue<>(queueSize);
this.batchSize = batchSize;
this.flushIntervalMs = flushIntervalMs;
this.dropPolicy = dropPolicy;
this.worker = new Thread(this::runWorker, "LoggerWorker");
this.worker.setDaemon(true);
}
void start() {
running.set(true);
worker.start();
}
void stop() {
running.set(false);
try {
worker.join();
} catch (InterruptedException ignored) {}
}
boolean enqueue(LogRecord record) {
try {
if ("drop_oldest".equals(dropPolicy)) {
while (!queue.offer(record)) {
queue.poll(); // drop oldest
dropped++;
}
} else if ("drop_new".equals(dropPolicy)) {
if (!queue.offer(record)) {
dropped++;
return false;
}
} else { // block briefly
queue.offer(record, 100, TimeUnit.MILLISECONDS);
}
return true;
} catch (Exception e) {
dropped++;
return false;
}
}
private void runWorker() {
List<LogRecord> batch = new ArrayList<>();
long lastFlush = System.currentTimeMillis();
while (running.get() || !queue.isEmpty()) {
try {
LogRecord rec = queue.poll(flushIntervalMs, TimeUnit.MILLISECONDS);
if (rec != null) batch.add(rec);
if (!batch.isEmpty() && (batch.size() >= batchSize ||
System.currentTimeMillis() - lastFlush >= flushIntervalMs)) {
flush(batch);
batch.clear();
lastFlush = System.currentTimeMillis();
}
} catch (Exception ignored) {}
}
if (!batch.isEmpty()) flush(batch);
}
private void flush(List<LogRecord> batch) {
for (LogRecord rec : batch) {
for (Handler h : handlers) {
h.handle(rec);
}
}
}
int getDroppedCount() {
return dropped;
}
}
/* ------------------------ Logger ------------------------ */
class Logger {
private final String name;
private final Level level;
private final AsyncDispatcher dispatcher;
Logger(String name, Level lvl, AsyncDispatcher disp) {
this.name = name;
this.level = lvl;
this.dispatcher = disp;
}
private void log(Level lvl, String msg, Map<String, Object> meta) {
if (lvl.getValue() < level.getValue()) return;
dispatcher.enqueue(new LogRecord(name, lvl, msg, meta));
}
void debug(String msg, Map<String, Object> meta) { log(Level.DEBUG, msg, meta); }
void info(String msg, Map<String, Object> meta) { log(Level.INFO, msg, meta); }
void warning(String msg, Map<String, Object> meta) { log(Level.WARNING, msg, meta); }
void error(String msg, Map<String, Object> meta) { log(Level.ERROR, msg, meta); }
void critical(String msg, Map<String, Object> meta) { log(Level.CRITICAL, msg, meta); }
}
/* ------------------------ DEMO MAIN ------------------------ */
public class LoggerSystem {
public static void main(String[] args) throws Exception {
Formatter fmt = new Formatter("{asctime} [{level}] {name}: {msg} {meta}");
ConsoleHandler console = new ConsoleHandler(Level.DEBUG, fmt);
RotatingFileHandler fileh = new RotatingFileHandler("logs/app.log", 2048, 3, Level.DEBUG, fmt);
AsyncDispatcher dispatcher = new AsyncDispatcher(
Arrays.asList(console, fileh), 200, 20, 200, "drop_oldest"
);
dispatcher.start();
Logger logger = new Logger("MyApp", Level.DEBUG, dispatcher);
ExecutorService pool = Executors.newFixedThreadPool(3);
for (int t = 0; t < 3; t++) {
int tid = t;
pool.submit(() -> {
for (int i = 0; i < 50; i++) {
Map<String, Object> meta = new HashMap<>();
meta.put("thread", tid);
meta.put("i", i);
logger.info("message " + i + " from thread " + tid, meta);
try { Thread.sleep(10); } catch (InterruptedException ignored) {}
}
});
}
pool.shutdown();
pool.awaitTermination(3, TimeUnit.SECONDS);
Thread.sleep(500);
dispatcher.stop();
System.out.println("Demo complete. Dropped messages: " + dispatcher.getDroppedCount());
}
}