1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package io.github.prolobjectlink.prolog.jtrolog;
23
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.logging.Formatter;
27 import java.util.logging.Level;
28 import java.util.logging.LogRecord;
29
30 import io.github.prolobjectlink.prolog.PrologFormatter;
31
32
33
34
35
36
37 final class JTrologFormatter extends Formatter implements PrologFormatter {
38
39 private static final String ERROR = "ERROR";
40 private static final String WARN = "WARN";
41 private static final String INFO = "INFO";
42 private static final String DEBUG = "DEBUG";
43 private static final String TRACE = "TRACE";
44
45 @Override
46 public String format(LogRecord record) {
47 StringBuilder buffer = new StringBuilder();
48
49
50 String levelString = "";
51 Level level = record.getLevel();
52 if (level.intValue() == Level.SEVERE.intValue()) {
53 levelString = ERROR;
54 } else if (level.intValue() == Level.WARNING.intValue()) {
55 levelString = WARN;
56 } else if (level.intValue() == Level.INFO.intValue()) {
57 levelString = INFO;
58 } else if (level.intValue() == Level.CONFIG.intValue()) {
59 levelString = DEBUG;
60 } else if (level.intValue() == Level.FINE.intValue()) {
61 levelString = DEBUG;
62 } else if (level.intValue() == Level.FINER.intValue()) {
63 levelString = DEBUG;
64 } else if (level.intValue() == Level.FINEST.intValue()) {
65 levelString = TRACE;
66 }
67
68 long millisecs = record.getMillis();
69 String name = record.getLoggerName();
70 String message = record.getMessage();
71 Throwable throwable = record.getThrown();
72 SimpleDateFormat f = new SimpleDateFormat("MMM-dd-yyyy HH:mm");
73 Date resultdate = new Date(millisecs);
74 String date = f.format(resultdate);
75
76
77 buffer.append('[');
78 buffer.append(name);
79 buffer.append(']');
80 buffer.append(' ');
81 buffer.append(levelString);
82 buffer.append(' ');
83 buffer.append(date);
84 buffer.append(' ');
85 buffer.append(message);
86 if (throwable != null) {
87 buffer.append('\n');
88 buffer.append(throwable);
89 buffer.append('\n');
90 StackTraceElement[] s = throwable.getStackTrace();
91 for (StackTraceElement stackTraceElement : s) {
92 buffer.append(stackTraceElement);
93 buffer.append('\n');
94 }
95 }
96 buffer.append('\n');
97 return "" + buffer + "";
98 }
99
100 }