public class BufferedLogger
extends java.lang.Object
implements java.lang.AutoCloseable
- it does not block caller on log(String) using daemon thread to queue all logging requests
- it holds lasst records in memory from where it could be easily obtained using slice(long, int), getLast(int) and getCopy() calls.
It is possible to connect a logger to a PrintStream using printTo(PrintStream, boolean).
It is also possible to capture the whole stdout with interceptStdOut()
| Modifier and Type | Class and Description |
|---|---|
static class |
BufferedLogger.Entry
Log entry structure provides ID to navigate, creation instant and the message.
|
| Constructor and Description |
|---|
BufferedLogger(int maxEntries)
Create buffered logger capable to hold in mempry up to specified number of entries, the excessive records will be
purged automatically.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
clear the whole stored content.
|
void |
close() |
@NonNull BufferedLogger.Entry |
d(java.lang.String message) |
void |
e(java.lang.String s) |
protected void |
finalize() |
void |
flush()
Wait until all queued logging operation are finished.
|
boolean |
flush(long millis)
Wait for log messages queue emptied up to specified number of milliseconds
|
java.util.List<BufferedLogger.Entry> |
getCopy()
Get a copy af all currently stored entries
|
@NonNull java.util.List<BufferedLogger.Entry> |
getLast(int maxEntries)
Return most recent record up to specified number of entries.
|
void |
interceptStdOut()
Start intercepting stdout and log it to the buffer on the fly.
|
@NonNull BufferedLogger.Entry |
log(java.lang.String message)
Add log entry to the buffer.
|
java.io.PrintStream |
printTo(java.io.PrintStream ps,
boolean printTimestamp)
Copy results to a given PrintStream (to be used with System.out, for example)
|
java.util.List<BufferedLogger.Entry> |
slice(long id,
int maxEntries)
Get slice of entries based on the id.
|
void |
stopInterceptingStdOut() |
public BufferedLogger(int maxEntries)
maxEntries - public void e(java.lang.String s)
public void flush()
throws java.lang.InterruptedException
log(String) fro details.java.lang.InterruptedExceptionpublic boolean flush(long millis)
millis - to stop waiting afterpublic java.io.PrintStream printTo(java.io.PrintStream ps,
boolean printTimestamp)
ps - printStream to print to. Use null to cancel.printTimestamp - true to add ISO timestamp to each line (like 2017-09-15T14:52:25.287Z)public @NonNull BufferedLogger.Entry log(java.lang.String message)
message - public @NonNull BufferedLogger.Entry d(java.lang.String message)
public @NonNull java.util.List<BufferedLogger.Entry> getLast(int maxEntries)
maxEntries - must be > 0. If current number of records is less than specified, returns all.public java.util.List<BufferedLogger.Entry> slice(long id, int maxEntries)
id - of the record to start withmaxEntries - and the direction, positive means newest than id (entry.id > id), negative for older (entry.id
< id).public void clear()
public java.util.List<BufferedLogger.Entry> getCopy()
public void interceptStdOut()
stopInterceptingStdOut() to cancel copying to log.public void close()
throws java.lang.Exception
close in interface java.lang.AutoCloseablejava.lang.Exceptionpublic void stopInterceptingStdOut()
protected void finalize()
throws java.lang.Throwable
finalize in class java.lang.Objectjava.lang.Throwable