This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
JsonLayoutBase.java
executable file
·161 lines (131 loc) · 4.67 KB
/
JsonLayoutBase.java
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
/**
* Copyright (C) 2016, The logback-contrib developers. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.contrib.json;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
/**
* @author Les Hazlewood
* @author Pierre Queinnec
* @author Espen A. Fossen
* @since 0.1
*/
public abstract class JsonLayoutBase<E> extends LayoutBase<E> {
public final static String CONTENT_TYPE = "application/json";
protected boolean includeTimestamp;
protected String timestampFormat;
protected String timestampFormatTimezoneId;
protected boolean appendLineSeparator;
protected JsonFormatter jsonFormatter;
public JsonLayoutBase() {
this.includeTimestamp = true;
this.appendLineSeparator = false;
}
@Override
public String doLayout(E event) {
Map map = toJsonMap(event);
if (map == null || map.isEmpty()) {
return null;
}
String result = getStringFromFormatter(map);
return isAppendLineSeparator() ? result + CoreConstants.LINE_SEPARATOR : result;
}
private String getStringFromFormatter(Map map) {
JsonFormatter formatter = getJsonFormatter();
if (formatter == null) {
addError("JsonFormatter has not been configured on JsonLayout instance " + getClass().getName() + ". Defaulting to map.toString().");
return map.toString();
}
try {
return formatter.toJsonString(map);
} catch (Exception e) {
addError("JsonFormatter failed. Defaulting to map.toString(). Message: " + e.getMessage(), e);
return map.toString();
}
}
protected String formatTimestamp(long timestamp) {
if (this.timestampFormat == null || timestamp < 0) {
return String.valueOf(timestamp);
}
Date date = new Date(timestamp);
DateFormat format = createDateFormat(this.timestampFormat);
if (this.timestampFormatTimezoneId != null) {
TimeZone tz = TimeZone.getTimeZone(this.timestampFormatTimezoneId);
format.setTimeZone(tz);
}
return format(date, format);
}
public void addMap(String key, boolean field, Map<String, ?> mapValue, Map<String, Object> map) {
if (field && mapValue != null && !mapValue.isEmpty()) {
map.put(key, mapValue);
}
}
public void addTimestamp(String key, boolean field, long timeStamp, Map<String, Object> map) {
if(field){
String formatted = formatTimestamp(timeStamp);
if (formatted != null) {
map.put(key, formatted);
}
}
}
public void add(String fieldName, boolean field, String value, Map<String, Object> map) {
if (field && value != null) {
map.put(fieldName, value);
}
}
protected DateFormat createDateFormat(String timestampFormat) {
return new SimpleDateFormat(timestampFormat);
}
protected String format(Date date, DateFormat format) {
return format.format(date);
}
protected abstract Map toJsonMap(E e);
@Override
public String getContentType() {
return CONTENT_TYPE;
}
public boolean isIncludeTimestamp() {
return includeTimestamp;
}
public void setIncludeTimestamp(boolean includeTimestamp) {
this.includeTimestamp = includeTimestamp;
}
public JsonFormatter getJsonFormatter() {
return jsonFormatter;
}
public void setJsonFormatter(JsonFormatter jsonFormatter) {
this.jsonFormatter = jsonFormatter;
}
public String getTimestampFormat() {
return timestampFormat;
}
public void setTimestampFormat(String timestampFormat) {
this.timestampFormat = timestampFormat;
}
public String getTimestampFormatTimezoneId() {
return timestampFormatTimezoneId;
}
public void setTimestampFormatTimezoneId(String timestampFormatTimezoneId) {
this.timestampFormatTimezoneId = timestampFormatTimezoneId;
}
public boolean isAppendLineSeparator() {
return appendLineSeparator;
}
public void setAppendLineSeparator(boolean appendLineSeparator) {
this.appendLineSeparator = appendLineSeparator;
}
}