mirror of
https://github.com/corda/corda.git
synced 2024-12-29 09:18:58 +00:00
9bb3d6b972
git-subtree-dir: sgx-jvm/avian git-subtree-mainline:f978eab8d1
git-subtree-split:09e4fe60d0
151 lines
3.7 KiB
Java
151 lines
3.7 KiB
Java
import java.util.logging.Handler;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.LogRecord;
|
|
import java.util.logging.Logger;
|
|
|
|
public class Logging {
|
|
private static final Logger log = Logger.getLogger("Logging");
|
|
|
|
private static class MyHandler extends Handler {
|
|
private static final int NAME_WIDTH = 18;
|
|
private static final int METHOD_WIDTH = 20;
|
|
private static final int LEVEL_WIDTH = 8;
|
|
|
|
public Object clone() { return this; }
|
|
public void close() { }
|
|
public void flush() { }
|
|
|
|
private void maybeLogThrown(StringBuilder sb, Throwable t) {
|
|
if (t != null) {
|
|
sb.append("\nCaused by: ");
|
|
sb.append(t.getClass().getName());
|
|
sb.append(": ");
|
|
sb.append(t.getMessage());
|
|
sb.append('\n');
|
|
|
|
for (StackTraceElement elt : t.getStackTrace()) {
|
|
sb.append('\t');
|
|
sb.append(elt.getClassName());
|
|
sb.append('.');
|
|
sb.append(elt.getMethodName());
|
|
sb.append('(');
|
|
sb.append(elt.getFileName());
|
|
sb.append(':');
|
|
sb.append(elt.getLineNumber());
|
|
sb.append(')');
|
|
sb.append('\n');
|
|
}
|
|
maybeLogThrown(sb, t.getCause());
|
|
}
|
|
}
|
|
|
|
private void indent(StringBuilder sb, int amount) {
|
|
do {
|
|
sb.append(' ');
|
|
} while (--amount > 0);
|
|
}
|
|
|
|
public void publish(LogRecord r) {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(r.getLoggerName());
|
|
indent(sb, NAME_WIDTH - r.getLoggerName().length());
|
|
String methodName = r.getSourceMethodName();
|
|
if (methodName == null) {
|
|
methodName = "<unknown>";
|
|
}
|
|
sb.append(methodName);
|
|
indent(sb, METHOD_WIDTH - methodName.length());
|
|
sb.append(r.getLevel().getName());
|
|
indent(sb, LEVEL_WIDTH - r.getLevel().getName().length());
|
|
sb.append(r.getMessage());
|
|
maybeLogThrown(sb, r.getThrown());
|
|
System.out.println(sb.toString());
|
|
}
|
|
}
|
|
|
|
public void run() {
|
|
log.info("Started run");
|
|
a();
|
|
log.log(Level.INFO, "Ended {}!", "run");
|
|
}
|
|
|
|
private void a() {
|
|
log.fine("Started a()");
|
|
b();
|
|
}
|
|
|
|
private void b() {
|
|
log.info("Started b()");
|
|
c();
|
|
}
|
|
|
|
private void c() {
|
|
log.warning("Started c()");
|
|
try {
|
|
d();
|
|
} catch (Exception ex) {
|
|
log.log(Level.SEVERE, "Exception caught in c", ex);
|
|
}
|
|
}
|
|
|
|
private void d() throws Exception {
|
|
e();
|
|
}
|
|
|
|
private void e() throws Exception {
|
|
throw new Exception("Started here");
|
|
}
|
|
|
|
private static void expect(boolean v) {
|
|
if (! v) throw new RuntimeException();
|
|
}
|
|
|
|
private static final boolean useCustomHandler = true;
|
|
public static void main(String args[]) {
|
|
if (useCustomHandler) {
|
|
Logger root = Logger.getLogger("");
|
|
root.addHandler(new MyHandler());
|
|
for (Handler h : root.getHandlers()) root.removeHandler(h);
|
|
root.addHandler(new MyHandler());
|
|
}
|
|
|
|
Logging me = new Logging();
|
|
me.run();
|
|
|
|
{ final boolean[] logged = new boolean[1];
|
|
Logger root = Logger.getLogger("");
|
|
for (Handler h : root.getHandlers()) root.removeHandler(h);
|
|
root.setLevel(Level.FINER);
|
|
root.addHandler(new Handler() {
|
|
public void publish(LogRecord r) {
|
|
logged[0] = true;
|
|
}
|
|
|
|
public void close() { }
|
|
public void flush() { }
|
|
});
|
|
|
|
Logger foo = Logger.getLogger("foo");
|
|
expect(foo.getLevel() == null);
|
|
expect(foo.getParent() == root);
|
|
|
|
foo.info("hi");
|
|
expect(logged[0]);
|
|
|
|
logged[0] = false;
|
|
foo.fine("hi");
|
|
expect(logged[0]);
|
|
|
|
logged[0] = false;
|
|
foo.finest("hi");
|
|
expect(! logged[0]);
|
|
|
|
root.setLevel(Level.FINEST);
|
|
|
|
logged[0] = false;
|
|
foo.finest("hi");
|
|
expect(logged[0]);
|
|
}
|
|
}
|
|
}
|