Skip to content

Commit 810c20f

Browse files
committed
v1.1.0 - now with added metadata
1 parent cf8309c commit 810c20f

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

README.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This small library provides an appender for [logback](https://logback.qos.ch) (a
66

77
The appender pushes log entries to LogDNA via HTTPS
88

9+
Logback's thread-bound storage, the MDC, is sent to LogDNA as metadata to be indexed and then searchable. (See screenshot, and more below)
10+
911
## How To
1012

1113
Logback uses XML file in known locations with the most common being `classpath:/logback.xml`
@@ -67,7 +69,7 @@ and has some sensible options for dealing with buffer management. [Read more her
6769
<dependency>
6870
<groupId>net._95point2.utils</groupId>
6971
<artifactId>logback-logdna</artifactId>
70-
<version>1.0.0</version>
72+
<version>1.1.0</version>
7173
</dependency>
7274

7375
#### Or, plain ol' download
@@ -76,19 +78,35 @@ Or just download the Jar and it's dependencies from https://github.com/robshep/l
7678

7779
## Configure
7880

79-
You can go to town on most other logback configurations but the LogDNA only has a couple of settings
81+
You can go to town on most other logback configurations but the LogDNA only has a couple of settings (shown as the default)
8082

8183
* `<appName>LogDNA-Logback-Test</appName>` set this for good log management in LogDNA
8284
* `<ingestKey>${LOGDNA_INGEST_KEY}</ingestKey>` signup to LogDNA and find this in your account profile
8385
* `<includeStacktrace>true</includeStacktrace>` this library can send multiline stacktraces (see image) - Raw syslog transport cannot
86+
* `<sendMDC>true</sendMDC>` copies over logback's Mapped Diagnostic Context [(MDC)](https://logback.qos.ch/manual/mdc.html) as LogDNA Metadata which are then indexed and searchable.
8487

8588
## More Info
8689

8790
* The log line displays the thread, the logger (class) and the message
88-
91+
* LogDNA's metadata is populated with the logger as an indexable/searchable property.
8992
* The HTTP Transport is done by the very lightweight [DavidWebb](https://github.com/hgoebl/DavidWebb) REST Library and so doesn't introduce bulky dependencies
9093

94+
## Using the MDC for Meta Data
95+
96+
The combination of logback's MDC and LogDNA's metadata support is pretty powerful and means you can correlate web-requests, or a userId, or something else that happens in a thread in your application.
97+
98+
For example, doing this _anywhere_ in your application...
99+
100+
MDC.put("customerId", "C-1");
101+
MDC.put("requestId", "cafebabe1");
102+
103+
... means that you can then go and search for say, **that** customer in logDNA like this:
104+
105+
meta.customerId:"C-1"
106+
107+
108+
91109

92-
## Screenshot of the LogDNA log viewer
110+
## Screenshot of the LogDNA log viewer, including extra metadata
93111

94-
![Optional Text](../master/src/test/resources/logdna.png)
112+
![The Spoils of these toils](../master/src/test/resources/logdna-meta.png)

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>net._95point2.utils</groupId>
55
<artifactId>logback-logdna</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.0.0</version>
7+
<version>1.1.0</version>
88
<name>logback-logdna</name>
99
<url>http://maven.apache.org</url>
1010
<properties>

src/main/java/net/_95point2/utils/LogDNAAppender.java

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.URLEncoder;
66
import java.net.UnknownHostException;
77
import java.util.HashMap;
8+
import java.util.Map.Entry;
89

910
import org.json.JSONArray;
1011
import org.json.JSONException;
@@ -33,6 +34,7 @@ public class LogDNAAppender extends UnsynchronizedAppenderBase<ILoggingEvent>
3334
*/
3435
private String appName;
3536
private boolean includeStacktrace = true;
37+
private boolean sendMDC = true;
3638

3739
public LogDNAAppender() {
3840
try {
@@ -81,6 +83,17 @@ protected void append(ILoggingEvent ev)
8183
line.put("app", this.appName);
8284
line.put("line", sb.toString());
8385

86+
JSONObject meta = new JSONObject();
87+
meta.put("logger", ev.getLoggerName());
88+
line.put("meta", meta);
89+
90+
if(this.sendMDC && !ev.getMDCPropertyMap().isEmpty()){
91+
for(Entry<String,String> entry : ev.getMDCPropertyMap().entrySet()){
92+
meta.put(entry.getKey(), entry.getValue());
93+
}
94+
}
95+
96+
8497
lines.put(line);
8598

8699
HashMap<String,Object> params = new HashMap<>();
@@ -114,6 +127,10 @@ public void setAppName(String appName) {
114127
public void setIngestKey(String ingestKey) {
115128
this.http.setDefaultHeader("apikey", ingestKey);
116129
}
130+
131+
public void setSendMDC(boolean sendMDC) {
132+
this.sendMDC = sendMDC;
133+
}
117134

118135
public void setIncludeStacktrace(boolean includeStacktrace) {
119136
this.includeStacktrace = includeStacktrace;

src/test/java/net/_95point2/utils/LogDNATest.java

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Test;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
6+
import org.slf4j.MDC;
67

78
public class LogDNATest
89
{
@@ -15,6 +16,9 @@ public class LogDNATest
1516
@Test
1617
public void testLogDNALogbackConf() throws InterruptedException
1718
{
19+
MDC.put("customerId", "C-1");
20+
MDC.put("requestId", "cafebabe1");
21+
1822
Logger logger = LoggerFactory.getLogger(LogDNATest.class);
1923

2024
logger.info("Okay");
@@ -24,5 +28,7 @@ public void testLogDNALogbackConf() throws InterruptedException
2428
logger.error("Ah Sh*t!", new RuntimeException("Bang!"));
2529

2630
logger.info("Well, Bye!");
31+
32+
Thread.sleep(2000);
2733
}
2834
}

src/test/resources/logdna-meta.png

126 KB
Loading

0 commit comments

Comments
 (0)