This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
GelfAppender.java
201 lines (183 loc) · 5.24 KB
/
GelfAppender.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
package gelf4j.logback;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.classic.util.LevelToSyslogSeverity;
import ch.qos.logback.core.AppenderBase;
import gelf4j.GelfConnection;
import gelf4j.GelfMessage;
import gelf4j.GelfMessageUtil;
import gelf4j.GelfTargetConfig;
import gelf4j.SyslogLevel;
import java.io.IOException;
import java.util.Map;
/**
* Responsible for Formatting a log event and sending it to the Server. Note that you can't swap in a different
* Layout since the GELF format is static.
*/
@SuppressWarnings( "UnusedDeclaration" )
public class GelfAppender<E> extends AppenderBase<E>
{
private final GelfTargetConfig _config = new GelfTargetConfig();
private GelfConnection _connection;
public GelfTargetConfig getConfig()
{
return _config;
}
public void setCompressedChunking( final boolean compressedChunking )
{
_config.setCompressedChunking( compressedChunking );
}
public void setPort( final int port )
{
_config.setPort( port );
}
public void setHost( final String host )
{
_config.setHost( host );
}
public void setCodecClass( final String codecClass )
{
_config.setCodecClass( codecClass );
}
public void setAdditionalFields( final String additionalFields )
{
_config.setAdditionalFields( additionalFields );
}
public void setDefaultFields( final String data )
{
_config.setDefaultFields( data );
}
@Override
public void start()
{
super.start();
try
{
_connection = _config.createConnection();
}
catch( final Exception e )
{
throw new RuntimeException( "Error initialising gelf connection", e );
}
}
@Override
public void stop()
{
if( null != _connection )
{
try
{
_connection.close();
}
catch ( final IOException ioe )
{
//Ignored
}
_connection = null;
}
super.stop();
}
/**
* The main append method. Takes the event that is being logged, formats if for GELF and then sends it over the wire
* to the log server
*
* @param logEvent The event that we are logging
*/
@Override
protected void append( final E logEvent )
{
try
{
_connection.send( toGelf( logEvent ) );
}
catch( RuntimeException e )
{
addError( "Error occurred: ", e );
throw e;
}
}
/**
* Converts a log event into GELF JSON.
*
* @param logEvent The log event we're converting
* @return The log event converted into GELF JSON
*/
private GelfMessage toGelf( final E logEvent )
{
final ILoggingEvent event = (ILoggingEvent) logEvent;
final String formattedMessage = event.getFormattedMessage();
final SyslogLevel level = SyslogLevel.values()[ LevelToSyslogSeverity.convert( event ) ];
final GelfMessage message = _connection.newMessage( level, formattedMessage, event.getTimeStamp() );
final Map<String, String> mdc = event.getMDCPropertyMap();
for( final Map.Entry<String, String> entry : _config.getAdditionalFields().entrySet() )
{
final String fieldName = entry.getValue();
final String key = entry.getKey();
if( GelfTargetConfig.FIELD_LOGGER_NAME.equals( fieldName ) )
{
GelfMessageUtil.setValue( message, key, event.getLoggerName() );
}
else if( GelfTargetConfig.FIELD_THREAD_NAME.equals( fieldName ) )
{
GelfMessageUtil.setValue( message, key, event.getThreadName() );
}
else if( GelfTargetConfig.FIELD_TIMESTAMP_MS.equals( fieldName ) )
{
GelfMessageUtil.setValue( message, key, message.getJavaTimestamp() );
}
else if( GelfTargetConfig.FIELD_EXCEPTION.equals( fieldName ) )
{
final IThrowableProxy proxy = event.getThrowableProxy();
if( null != proxy )
{
final StringBuilder sb = new StringBuilder();
collectExceptionMessage( proxy, sb );
GelfMessageUtil.setValue( message, key, sb.toString() );
}
}
else if( null != mdc )
{
final String value = mdc.get( key );
if( null != value )
{
GelfMessageUtil.setValue( message, key, value );
}
}
}
message.getAdditionalFields().putAll( _config.getDefaultFields() );
return message;
}
private void collectExceptionMessage( final IThrowableProxy proxy, final StringBuilder sb )
{
sb.append( proxy.getClassName() );
final String m = proxy.getMessage();
if( null != m )
{
sb.append( ": " );
sb.append( m );
}
sb.append( "\n" );
for ( final StackTraceElementProxy element : proxy.getStackTraceElementProxyArray() )
{
sb.append( "\t" );
sb.append( element.getSTEAsString() );
sb.append( "\n" );
}
final IThrowableProxy cause = proxy.getCause();
if( null != cause )
{
sb.append( "Caused by: " );
collectExceptionMessage( cause, sb );
}
}
private String toStackTraceString( final StackTraceElementProxy[] elements )
{
final StringBuilder str = new StringBuilder();
for( final StackTraceElementProxy element : elements )
{
str.append( element.getSTEAsString() );
}
return str.toString();
}
}