-
Notifications
You must be signed in to change notification settings - Fork 8
/
LogRecord.java
243 lines (219 loc) · 8.06 KB
/
LogRecord.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright (C) 2020 2024 gregory higgins.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package com.fluxtion.runtime.audit;
import com.fluxtion.runtime.event.Event;
import com.fluxtion.runtime.time.Clock;
import lombok.Getter;
import lombok.Setter;
/**
* A structured log record that can be easily converted to a long term store,
* such as a rdbms for later analysis. The LogRecord creates a yaml
* representation of the LogRecord for simplified marshaling.
* <br>
* <p>
* A log record is triggered when an event is processed and written at the end
* of the execution loop. A sample record:
*
* <pre>
* eventLogRecord:
* eventTime: 1578236915413
* logTime: 1578236915413
* groupingId: null
* event: CharEvent
* nodeLogs:
* - parentNode_1: { char: a, prop: 32}
* - childNode_3: { child: true}
* endTime: 1578236915413
* <pre>
* <ul>
* <li>eventTime: the time the event was created
* <li>logTime: the time the log record is created i.e. when the event processing began
* <li>endTime: the time the log record is complete i.e. when the event processing completed
* <li>groupingId: a set of nodes can have a groupId and their configuration is controlled as a group
* <li>event: The simple class name of the event that created the execution
* <li>nodeLogs: are recorded in the order the event wave passes through the graph.
* Only nodes that on the execution path have an entry in the nodeList element.
* The key of a node is the instance name in the java code, followed by a set of key/value properties the node logs.
* </ul>
*
* @author Greg Higgins (greg.higgins@v12technology.com)
*/
public class LogRecord {
/**
* The id of the instance producing the record. GroupingId can be used to
* group LogRecord's together.
*/
public String groupingId;
@Getter
private EventLogControlEvent.LogLevel logLevel;
protected final StringBuilder sb;
protected String sourceId;
protected boolean firstProp;
@Setter
protected Clock clock;
protected boolean printEventToString = false;
@Setter
protected boolean printThreadName = false;
public LogRecord(Clock clock) {
this(clock, EventLogControlEvent.LogLevel.INFO);
}
public LogRecord(Clock clock, EventLogControlEvent.LogLevel logLevel) {
sb = new StringBuilder();
firstProp = true;
this.clock = clock;
updateLogLevel(logLevel);
}
public void updateLogLevel(EventLogControlEvent.LogLevel newLogLevel) {
this.logLevel = newLogLevel == null ? EventLogControlEvent.LogLevel.NONE : newLogLevel;
if (!loggingEnabled()) {
sb.setLength(0);
}
}
public void replaceBuffer(CharSequence newBuffer) {
sb.setLength(0);
sb.append(newBuffer);
}
public void addRecord(String sourceId, String propertyKey, double value) {
addSourceId(sourceId, propertyKey);
sb.append(value);
}
public void addRecord(String sourceId, String propertyKey, long value) {
addSourceId(sourceId, propertyKey);
sb.append(value);
}
public void addRecord(String sourceId, String propertyKey, int value) {
addSourceId(sourceId, propertyKey);
sb.append(value);
}
public void addRecord(String sourceId, String propertyKey, char value) {
addSourceId(sourceId, propertyKey);
sb.append(value);
}
public void addRecord(String sourceId, String propertyKey, CharSequence value) {
addSourceId(sourceId, propertyKey);
sb.append(value);
}
public void addRecord(String sourceId, String propertyKey, Object value) {
addSourceId(sourceId, propertyKey);
sb.append(value == null ? "NULL" : value);
}
public void addRecord(String sourceId, String propertyKey, boolean value) {
addSourceId(sourceId, propertyKey);
sb.append(value);
}
public void addTrace(String sourceId) {
if (this.sourceId != null) {
sb.append("}");
}
firstProp = true;
this.sourceId = null;
addSourceId(sourceId, null);
}
public void printEventToString(boolean printEventToString) {
this.printEventToString = printEventToString;
}
protected void addSourceId(String sourceId, String propertyKey) {
if (loggingEnabled()) {
if (this.sourceId == null) {
sb.append("\n - ").append(sourceId).append(": {");
this.sourceId = sourceId;
} else if (!this.sourceId.equals(sourceId)) {
sb.append("}\n - ").append(sourceId).append(": {");
this.sourceId = sourceId;
firstProp = true;
}
if (!firstProp) {
sb.append(",");
}
if (propertyKey != null) {
firstProp = false;
sb.append(" ").append(propertyKey).append(": ");
}
}
}
public void clear() {
firstProp = true;
sourceId = null;
sb.setLength(0);
}
public CharSequence asCharSequence() {
return sb;
}
public void triggerEvent(Event event) {
if (loggingEnabled()) {
Class<? extends Event> aClass = event.getClass();
sb.append("eventLogRecord: ");
sb.append("\n eventTime: ").append(clock.getEventTime());
sb.append("\n logTime: ").append(clock.getWallClockTime());
sb.append("\n groupingId: ").append(groupingId);
sb.append("\n event: ").append(aClass.getSimpleName());
if (printEventToString) {
sb.append("\n eventToString: ").append(event.toString());
}
if (printThreadName) {
sb.append("\n thread: ").append(Thread.currentThread().getName());
}
if (event.filterString() != null && !event.filterString().isEmpty()) {
sb.append("\n eventFilter: ").append(event.filterString());
}
sb.append("\n nodeLogs: ");
}
}
public void triggerObject(Object event) {
if (loggingEnabled()) {
if (event instanceof Event) {
triggerEvent((Event) event);
} else {
Class<?> aClass = event.getClass();
sb.append("eventLogRecord: ");
sb.append("\n eventTime: ").append(clock.getEventTime());
sb.append("\n logTime: ").append(clock.getWallClockTime());
sb.append("\n groupingId: ").append(groupingId);
sb.append("\n event: ").append(aClass.getSimpleName());
if (printEventToString) {
sb.append("\n eventToString: ").append(event.toString());
}
sb.append("\n nodeLogs: ");
}
}
}
/**
* complete record processing, the return value indicates if any log values
* were written.
*
* @return flag to indicate properties were logged
*/
public boolean terminateRecord() {
boolean logged = !firstProp;
if (loggingEnabled()) {
if (this.sourceId != null) {
sb.append("}");
}
sb.append("\n endTime: ").append(clock.getWallClockTime());
}
firstProp = true;
sourceId = null;
return logged;
}
@Override
public String toString() {
return asCharSequence().toString();
}
protected boolean loggingEnabled() {
return logLevel != EventLogControlEvent.LogLevel.NONE;
}
}