Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add shortMessagePattern configuration #21

Merged
merged 1 commit into from

2 participants

@ibuildthecloud

As an alternative to truncating the full message to create a short message
you can use logback's pattern layout to construct the short message. This
gives far more flexibility in terms of what gets constructed.

@ibuildthecloud ibuildthecloud Add shortMessagePattern configuration
As an alternative to truncating the full message to create a short message
you can use logback's pattern layout to construct the short message.  This
gives far more flexibility in terms of what gets constructed.
57d3eda
@Moocar Moocar merged commit 57d3eda into Moocar:master
@Moocar
Owner

Great idea! Added a test and sample data as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2013
  1. @ibuildthecloud

    Add shortMessagePattern configuration

    ibuildthecloud authored
    As an alternative to truncating the full message to create a short message
    you can use logback's pattern layout to construct the short message.  This
    gives far more flexibility in terms of what gets constructed.
This page is out of date. Refresh to see the latest.
View
3  README.md
@@ -69,6 +69,9 @@ changed from 0.9.5 -> 0.9.6. Allowed values = 0.9.5 and 0.9.6. Defaults to "0.9.
smaller packets. Defaults to 1000
* **messagePattern**: The layout of the actual message according to
[PatternLayout](http://logback.qos.ch/manual/layouts.html#conversionWord). Defaults to "%m%rEx"
+* **shortMessagePattern**: The layout of the short message according to
+[PatternLayout](http://logback.qos.ch/manual/layouts.html#conversionWord). Defaults to none which means the message will
+be truncated to create the short message
* **additionalFields**: See additional fields below. Defaults to empty
* **staticAdditionalFields**: See static additional fields below. Defaults to empty
* **includeFullMDC**: See additional fields below. Defaults to false
View
12 src/main/java/me/moocar/logbackgelf/GelfAppender.java
@@ -29,6 +29,7 @@
private String graylog2ServerVersion = "0.9.6";
private int chunkThreshold = 1000;
private String messagePattern = "%m%rEx";
+ private String shortMessagePattern = null;
private Map<String, String> additionalFields = new HashMap<String, String>();
private Map<String, String> staticAdditionalFields = new HashMap<String, String>();
private boolean includeFullMDC;
@@ -99,7 +100,8 @@ private void initExecutor() {
new ChunkFactory(chunkedGelfId, padSeq));
GelfConverter converter = new GelfConverter(facility, useLoggerName, useThreadName, additionalFields,
- staticAdditionalFields, shortMessageLength, hostname, messagePattern, includeFullMDC);
+ staticAdditionalFields, shortMessageLength, hostname, messagePattern, shortMessagePattern,
+ includeFullMDC);
appenderExecutor = new AppenderExecutor(transport, payloadChunker, converter, new Zipper(), chunkThreshold);
@@ -322,4 +324,12 @@ public String getMessagePattern() {
public void setMessagePattern(String messagePattern) {
this.messagePattern = messagePattern;
}
+
+ public String getShortMessagePattern() {
+ return shortMessagePattern;
+ }
+
+ public void setShortMessagePattern(String shortMessagePattern) {
+ this.shortMessagePattern = shortMessagePattern;
+ }
}
View
19 src/main/java/me/moocar/logbackgelf/GelfConverter.java
@@ -30,6 +30,7 @@
private final String hostname;
private final Gson gson;
private final PatternLayout patternLayout;
+ private final PatternLayout shortPatternLayout;
private boolean includeFullMDC;
public GelfConverter(String facility,
@@ -40,6 +41,7 @@ public GelfConverter(String facility,
int shortMessageLength,
String hostname,
String messagePattern,
+ String shortMessagePattern,
boolean includeFullMDC) {
this.facility = facility;
@@ -59,6 +61,15 @@ public GelfConverter(String facility,
this.patternLayout.setContext(new LoggerContext());
this.patternLayout.setPattern(messagePattern);
this.patternLayout.start();
+
+ if ( shortMessagePattern == null ) {
+ this.shortPatternLayout = null;
+ } else {
+ this.shortPatternLayout = new PatternLayout();
+ this.shortPatternLayout.setContext(new LoggerContext());
+ this.shortPatternLayout.setPattern(shortMessagePattern);
+ this.shortPatternLayout.start();
+ }
}
/**
@@ -91,7 +102,7 @@ public String toGelf(ILoggingEvent logEvent) {
String message = patternLayout.doLayout(logEvent);
map.put("full_message", message);
- map.put("short_message", truncateToShortMessage(message));
+ map.put("short_message", truncateToShortMessage(message, logEvent));
// Ever since version 0.9.6, GELF accepts timestamps in decimal form.
double logEventTimeTimeStamp = logEvent.getTimeStamp() / 1000.0;
@@ -172,7 +183,11 @@ private void staticAdditionalFields(Map<String,Object> map) {
}
}
- private String truncateToShortMessage(String fullMessage) {
+ private String truncateToShortMessage(String fullMessage, ILoggingEvent logEvent) {
+ if ( shortPatternLayout != null ) {
+ return shortPatternLayout.doLayout(logEvent);
+ }
+
if (fullMessage.length() > shortMessageLength) {
return fullMessage.substring(0, shortMessageLength);
}
Something went wrong with that request. Please try again.