Provides the same information as the one line format but using JSON formatting. All the information of the LogRecord is included as a one line JSON document, including the full stack trace of the associated exception if any. The LogRecord is mapped as attributes: time: the log record ti
| 37 | * </ul> |
| 38 | */ |
| 39 | public class JsonFormatter extends OneLineFormatter { |
| 40 | |
| 41 | private static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSX"; |
| 42 | |
| 43 | /** |
| 44 | * Constructs a new JsonFormatter. |
| 45 | */ |
| 46 | public JsonFormatter() { |
| 47 | String timeFormat = LogManager.getLogManager().getProperty(JsonFormatter.class.getName() + ".timeFormat"); |
| 48 | if (timeFormat == null) { |
| 49 | timeFormat = DEFAULT_TIME_FORMAT; |
| 50 | } |
| 51 | setTimeFormat(timeFormat); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String format(LogRecord record) { |
| 56 | StringBuilder sb = new StringBuilder(); |
| 57 | sb.append('{'); |
| 58 | |
| 59 | // Timestamp |
| 60 | sb.append("\"time\": \""); |
| 61 | addTimestamp(sb, record.getMillis()); |
| 62 | sb.append("\", "); |
| 63 | |
| 64 | // Severity |
| 65 | sb.append("\"level\": \""); |
| 66 | sb.append(record.getLevel().getLocalizedName()); |
| 67 | sb.append("\", "); |
| 68 | |
| 69 | // Thread |
| 70 | sb.append("\"thread\": \""); |
| 71 | sb.append(resolveThreadName(record)); |
| 72 | sb.append("\", "); |
| 73 | |
| 74 | // Source |
| 75 | sb.append("\"class\": \""); |
| 76 | sb.append(record.getSourceClassName()); |
| 77 | sb.append("\", "); |
| 78 | sb.append("\"method\": \""); |
| 79 | sb.append(record.getSourceMethodName()); |
| 80 | sb.append("\", "); |
| 81 | |
| 82 | // Message |
| 83 | sb.append("\"message\": \""); |
| 84 | sb.append(JSONFilter.escape(formatMessage(record))); |
| 85 | |
| 86 | Throwable t = record.getThrown(); |
| 87 | if (t != null) { |
| 88 | sb.append("\", "); |
| 89 | |
| 90 | // Stack trace |
| 91 | sb.append("\"throwable\": ["); |
| 92 | boolean first = true; |
| 93 | do { |
| 94 | if (!first) { |
| 95 | sb.append(','); |
| 96 | } else { |
nothing calls this directly
no outgoing calls
no test coverage detected