Skip to content

Commit 43f6118

Browse files
authored
Merge pull request #8 from bufferapp/task/log-autoinject
Take advantage of dd-trace `autoinject` feature
2 parents 77c9307 + 0d8c909 commit 43f6118

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,25 @@ If you wish to see more logs, simply set the `LOG_LEVEL` to the desired level. H
2323
| NOTICE | Logs that track the general flow of the application. This is the default level | |
2424
| WARNING | Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop. | |
2525
| ERROR | Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure. | |
26-
| CRITICAL | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. | |
26+
| CRITICAL | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. | |
27+
28+
29+
## Add traces to log
30+
31+
A great feature of Datadog is to correlate traces and logs to make troubleshooting easier.
32+
33+
To take advantage of this, you will need to:
34+
- install the `dd-trace` package
35+
- import it and init it with `logInjection:true`.
36+
- BuffLog will append automatically the traces to the logs (only within a request)
37+
38+
```js
39+
// make sure to put those lines at the very beginning of your service
40+
import tracer from "dd-trace";
41+
tracer.init({
42+
// will automatically append the traces to BuffLog
43+
logInjection: true
44+
45+
// ... all other options...
46+
});
47+
```

bufflog.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import tracer from "dd-trace";
2-
import formats from "dd-trace/ext/formats";
31

42
export class BuffLog {
53
pinoLogger: any;
6-
defaultLevel = String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) || "notice";;
4+
defaultLevel = process.env.LOG_LEVEL ? String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) : "notice";
75

86
constructor() {
97
this.pinoLogger = require('pino')({
@@ -16,19 +14,6 @@ export class BuffLog {
1614
// soon: remove the `v` field https://github.com/pinojs/pino/issues/620
1715
base: {},
1816

19-
mixin () {
20-
// Check here if a current trace exist to inject it in the log
21-
// `tracer` is a singleton, will no-op if no tracer was initialized
22-
var span = tracer.scope().active()
23-
if (span) {
24-
const traceInfo = {}
25-
tracer.inject(span.context(), formats.LOG, traceInfo);
26-
return traceInfo;
27-
} else {
28-
return {}
29-
}
30-
},
31-
3217
// notice doesn't exist in pino, let's add it
3318
customLevels: {
3419
notice: 35

index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import tracer from "dd-trace";
22
import express from 'express';
3-
import bufflog from './bufflog';
3+
import bufflog from './bufflog';
44

55
tracer.init({
66
hostname: "dd-agent-hostname",
7-
logInjection: false
7+
// will automatically append the traces to BuffLog
8+
logInjection: true
89
});
910

1011
bufflog.info('hello info');

0 commit comments

Comments
 (0)