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

Add customizable keys for the logger name and thread name #42

Merged
merged 1 commit into from
Dec 1, 2019
Merged
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ Configuration
Default: false.
* **includeLevelName**: If true, the log level name (e.g. DEBUG) will be sent, too. Default: false.
* **levelNameKey**: The key (i.e. the field name) that should be used for the log level name.
This is only relevant when includeLevelName is trueö. Default: level_name.
This is only relevant when includeLevelName is true. Default: level_name.
* **loggerNameKey**: The key (i.e. the field name) that should be used for the logger name.
Default: logger_name.
* **threadNameKey**: The key (i.e. the field name) that should be used for the thread name.
Default: thread_name.
* **appendNewline**: If true, a system depended newline separator will be added at the end of each message.
Don't use this in conjunction with TCP or UDP appenders, as this is only reasonable for
console logging!
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/de/siegmar/logbackgelf/GelfEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public class GelfEncoder extends EncoderBase<ILoggingEvent> {
*/
private String levelNameKey = "level_name";

/**
* The key that should be used for the loggerName.
*/
private String loggerNameKey = "logger_name";

/**
* The key that should be used for the threadName.
*/
private String threadNameKey = "thread_name";

/**
* If true, a system depended newline separator will be added at the end of each message.
* Don't use this in conjunction with TCP or UDP appenders, as this is only reasonable for
Expand Down Expand Up @@ -181,6 +191,22 @@ public void setLevelNameKey(final String levelNameKey) {
this.levelNameKey = levelNameKey;
}

public String getLoggerNameKey() {
return loggerNameKey;
}

public void setLoggerNameKey(final String loggerNameKey) {
this.loggerNameKey = loggerNameKey;
}

public String getThreadNameKey() {
return threadNameKey;
}

public void setThreadNameKey(final String threadNameKey) {
this.threadNameKey = threadNameKey;
}

public boolean isAppendNewline() {
return appendNewline;
}
Expand Down Expand Up @@ -322,8 +348,8 @@ public byte[] footerBytes() {
private Map<String, Object> mapAdditionalFields(final ILoggingEvent event) {
final Map<String, Object> additionalFields = new HashMap<>(staticFields);

additionalFields.put("logger_name", event.getLoggerName());
additionalFields.put("thread_name", event.getThreadName());
additionalFields.put(loggerNameKey, event.getLoggerName());
additionalFields.put(threadNameKey, event.getThreadName());

if (includeRawMessage) {
additionalFields.put("raw_message", event.getMessage());
Expand Down
47 changes: 45 additions & 2 deletions src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ private LoggingEvent simpleLoggingEvent(final Logger logger, final Throwable e)
new Object[]{1});
}

private void basicValidation(final JsonNode jsonNode) {
private void coreValidation(final JsonNode jsonNode) {
assertEquals("1.1", jsonNode.get("version").textValue());
assertEquals("localhost", jsonNode.get("host").textValue());
assertEquals("message 1", jsonNode.get("short_message").textValue());
assertEquals(7, jsonNode.get("level").intValue());
}

private void basicValidation(final JsonNode jsonNode) {
coreValidation(jsonNode);
assertNotNull(jsonNode.get("_thread_name").textValue());
assertEquals(LOGGER_NAME, jsonNode.get("_logger_name").textValue());
}
Expand Down Expand Up @@ -182,7 +186,6 @@ public void complex() throws IOException {
assertNull(jsonNode.get("_exception"));
}


@Test
public void customLevelNameKey() throws IOException {
encoder.setIncludeLevelName(true);
Expand All @@ -203,6 +206,46 @@ public void customLevelNameKey() throws IOException {
assertNull(jsonNode.get("_exception"));
}

@Test
public void customLoggerNameKey() throws IOException {
encoder.setLoggerNameKey("Logger");
encoder.start();

final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger logger = lc.getLogger(LOGGER_NAME);

final LoggingEvent event = simpleLoggingEvent(logger, null);

final String logMsg = encodeToStr(event);

final ObjectMapper om = new ObjectMapper();
final JsonNode jsonNode = om.readTree(logMsg);
coreValidation(jsonNode);
assertNotNull(jsonNode.get("_thread_name").textValue());
assertEquals(LOGGER_NAME, jsonNode.get("_Logger").textValue());
assertNull(jsonNode.get("_exception"));
}

@Test
public void customThreadNameKey() throws IOException {
encoder.setThreadNameKey("Thread");
encoder.start();

final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger logger = lc.getLogger(LOGGER_NAME);

final LoggingEvent event = simpleLoggingEvent(logger, null);

final String logMsg = encodeToStr(event);

final ObjectMapper om = new ObjectMapper();
final JsonNode jsonNode = om.readTree(logMsg);
coreValidation(jsonNode);
assertNotNull(jsonNode.get("_Thread").textValue());
assertEquals(LOGGER_NAME, jsonNode.get("_logger_name").textValue());
assertNull(jsonNode.get("_exception"));
}

@Test
public void rootExceptionTurnedOff() throws IOException {
encoder.start();
Expand Down