Skip to content

Commit 63d7452

Browse files
authored
Include stack traces and arguments in logs sent to Better Stack (#14)
1 parent 869ee42 commit 63d7452

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

examples/gradle/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
* Get your source token at [Better Stack -> Sources](http://logs.betterstack.com/team/0/sources).
44
* Edit [src/main/resources/logback.xml](src/main/resources/logback.xml) and replace `<!-- YOUR SOURCE TOKEN -->` with your source token.
55
* Run `gradle run` from this directory.
6-
* You should see a `Hello world` log in [Better Stack -> Live tail](https://logs.betterstack.com/team/0/tail).
6+
* You should see a `Hello World!` log along with a few other examples in [Better Stack -> Live tail](https://logs.betterstack.com/team/0/tail).
77

examples/gradle/src/main/java/com/logtail/example/App.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import org.slf4j.MDC;
56

7+
import java.awt.Point;
8+
import java.time.Instant;
69
import java.util.concurrent.TimeUnit;
10+
import java.util.Random;
11+
import java.util.UUID;
712

813
public class App {
914
public static void main(String[] args) {
1015
Logger logger = LoggerFactory.getLogger(App.class);
11-
logger.info("hello world");
16+
17+
MDC.put("requestId", UUID.randomUUID().toString());
18+
MDC.put("requestTime", Long.toString(Instant.now().getEpochSecond()));
19+
20+
logger.info("Hello World!");
21+
22+
Random coordinateGenerator = new Random();
23+
logger.info("Point A", new Point(coordinateGenerator.nextInt(1024), coordinateGenerator.nextInt(1024)));
24+
logger.info("Point B", new Point(coordinateGenerator.nextInt(1024), coordinateGenerator.nextInt(1024)));
25+
logger.info("Point C", new Point(coordinateGenerator.nextInt(1024), coordinateGenerator.nextInt(1024)));
26+
27+
logger.info("Snow White met a few dwarfs.", "Doc", "Sleepy", "Dopey", "Grumpy", "Happy", "Bashful", "Sneezy");
28+
29+
try {
30+
throw new RuntimeException("An error occurred.", new Exception("The original cause."));
31+
} catch (RuntimeException exception) {
32+
logger.error("Logging a thrown exception.", exception);
33+
}
1234

1335
try {
1436
TimeUnit.SECONDS.sleep(5);

examples/maven/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* Get your source token at [Better Stack -> Sources](http://logs.betterstack.com/team/0/sources).
44
* Edit [src/main/resources/logback.xml](src/main/resources/logback.xml) and replace `<!-- YOUR SOURCE TOKEN -->` with your source token.
55
* Run `mvn compile -e exec:java -Dexec.mainClass="com.logtail.example.App"` from this directory.
6-
* You should see a "Hello world" log in [Better Stack -> Live tail](https://logs.betterstack.com/team/0/tail).
6+
* You should see a `Hello World!` log along with a few other examples in [Better Stack -> Live tail](https://logs.betterstack.com/team/0/tail).

examples/maven/src/main/java/com/logtail/example/App.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import org.slf4j.MDC;
6+
7+
import java.awt.Point;
8+
import java.time.Instant;
9+
import java.util.Random;
10+
import java.util.UUID;
511

612
public class App {
713
public static void main(String[] args) {
814
Logger logger = LoggerFactory.getLogger(App.class);
9-
logger.info("hello world");
15+
16+
MDC.put("requestId", UUID.randomUUID().toString());
17+
MDC.put("requestTime", Long.toString(Instant.now().getEpochSecond()));
18+
19+
logger.info("Hello World!");
20+
21+
Random coordinateGenerator = new Random();
22+
logger.info("Point A", new Point(coordinateGenerator.nextInt(1024), coordinateGenerator.nextInt(1024)));
23+
logger.info("Point B", new Point(coordinateGenerator.nextInt(1024), coordinateGenerator.nextInt(1024)));
24+
logger.info("Point C", new Point(coordinateGenerator.nextInt(1024), coordinateGenerator.nextInt(1024)));
25+
26+
logger.info("Snow White met a few dwarfs.", "Doc", "Sleepy", "Dopey", "Grumpy", "Happy", "Bashful", "Sneezy");
27+
28+
try {
29+
throw new RuntimeException("An error occurred.", new Exception("The original cause."));
30+
} catch (RuntimeException exception) {
31+
logger.error("Logging a thrown exception.", exception);
32+
}
1033
}
1134
}

src/main/java/com/logtail/logback/LogtailAppender.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
44
import ch.qos.logback.classic.spi.ILoggingEvent;
5+
import ch.qos.logback.classic.spi.IThrowableProxy;
56
import ch.qos.logback.core.UnsynchronizedAppenderBase;
67
import com.fasterxml.jackson.annotation.JsonInclude;
78
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -183,6 +184,10 @@ protected Map<String, Object> buildPostData(ILoggingEvent event) {
183184
logLine.put("message", generateLogMessage(event));
184185
logLine.put("meta", generateLogMeta(event));
185186
logLine.put("runtime", generateLogRuntime(event));
187+
logLine.put("args", event.getArgumentArray());
188+
if (event.getThrowableProxy() != null) {
189+
logLine.put("throwable", generateLogThrowable(event.getThrowableProxy()));
190+
}
186191

187192
return logLine;
188193
}
@@ -227,6 +232,18 @@ protected Map<String, Object> generateLogRuntime(ILoggingEvent event) {
227232
return logRuntime;
228233
}
229234

235+
protected Map<String, Object> generateLogThrowable(IThrowableProxy throwable) {
236+
Map<String, Object> logThrowable = new HashMap<>();
237+
logThrowable.put("message", throwable.getMessage());
238+
logThrowable.put("class", throwable.getClassName());
239+
logThrowable.put("stackTrace", throwable.getStackTraceElementProxyArray());
240+
if (throwable.getCause() != null) {
241+
logThrowable.put("cause", generateLogThrowable(throwable.getCause()));
242+
}
243+
244+
return logThrowable;
245+
}
246+
230247
protected Object getMetaValue(String type, String value) {
231248
try {
232249
switch (type) {

0 commit comments

Comments
 (0)