Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved issue #105: Added GelfLogRecord class which allows passing #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/org/graylog2/logging/GelfHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.HashMap;
import java.util.IllegalFormatConversionException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.*;

public class GelfHandler
Expand Down Expand Up @@ -215,6 +216,13 @@ private GelfMessage makeMessage(final LogRecord record) {
String.valueOf(levelToSyslogLevel(record.getLevel())));
gelfMessage.addField("SourceClassName", record.getSourceClassName());
gelfMessage.addField("SourceMethodName", record.getSourceMethodName());

if (record instanceof GelfLogRecord) {
GelfLogRecord gelfLogRecord = (GelfLogRecord)record;
for (Entry<String, Object> field : gelfLogRecord.getFields().entrySet()) {
gelfMessage.addField(field.getKey(), field.getValue());
}
}

if (null != getOriginHost()) {
gelfMessage.setHost(getOriginHost());
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/graylog2/logging/GelfLogRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.graylog2.logging;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.LogRecord;

public class GelfLogRecord extends LogRecord {
private static final long serialVersionUID = 43242341L;
private Map<String, Object> fields;

public GelfLogRecord(Level level, String msg) {
super(level, msg);
}

public void setField(String key, Object value) {
if (fields == null) {
fields = new LinkedHashMap<String, Object>();
}
fields.put(key, value);
}

public Object getField(String key) {
return fields != null ? fields.get(key) : null;
}

public Map<String, Object> getFields() {
if (fields == null) {
return Collections.emptyMap();
}
return Collections.unmodifiableMap(fields);
}
}