Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from mjakl/master
Browse files Browse the repository at this point in the history
Add option to append a line separator after each JSON log message
  • Loading branch information
Christian Trutz committed Feb 3, 2013
2 parents a887f3f + 5d7a5c0 commit d5a03dd
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package ch.qos.logback.contrib.json;

import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;

import java.text.DateFormat;
Expand All @@ -35,11 +36,13 @@ public abstract class JsonLayoutBase<E> extends LayoutBase<E> {
protected boolean includeTimestamp;
protected String timestampFormat;
protected String timestampFormatTimezoneId;
protected boolean appendLineSeparator;

protected JsonFormatter jsonFormatter;

public JsonLayoutBase() {
this.includeTimestamp = true;
this.appendLineSeparator = false;
}

@Override
Expand All @@ -48,11 +51,17 @@ public String doLayout(E 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) {
Expand Down Expand Up @@ -122,4 +131,12 @@ public String getTimestampFormatTimezoneId() {
public void setTimestampFormatTimezoneId(String timestampFormatTimezoneId) {
this.timestampFormatTimezoneId = timestampFormatTimezoneId;
}

public boolean isAppendLineSeparator() {
return appendLineSeparator;
}

public void setAppendLineSeparator(boolean appendLineSeparator) {
this.appendLineSeparator = appendLineSeparator;
}
}

0 comments on commit d5a03dd

Please sign in to comment.