diff --git a/README.md b/README.md index 76a1fd5..3bfa987 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,25 @@ If you wish to see more logs, simply set the `LOG_LEVEL` to the desired level. H | NOTICE | Logs that track the general flow of the application. This is the default level | | | WARNING | Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop. | | | 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. | | -| CRITICAL | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. | | \ No newline at end of file +| CRITICAL | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. | | + + +## Add traces to log + +A great feature of Datadog is to correlate traces and logs to make troubleshooting easier. + +To take advantage of this, you will need to: +- install the `dd-trace` package +- import it and init it with `logInjection:true`. +- BuffLog will append automatically the traces to the logs (only within a request) + +```js +// make sure to put those lines at the very beginning of your service +import tracer from "dd-trace"; +tracer.init({ + // will automatically append the traces to BuffLog + logInjection: true + + // ... all other options... +}); +``` diff --git a/bufflog.ts b/bufflog.ts index bb4aed3..5faa7dc 100644 --- a/bufflog.ts +++ b/bufflog.ts @@ -1,9 +1,7 @@ -import tracer from "dd-trace"; -import formats from "dd-trace/ext/formats"; export class BuffLog { pinoLogger: any; - defaultLevel = String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) || "notice";; + defaultLevel = process.env.LOG_LEVEL ? String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) : "notice"; constructor() { this.pinoLogger = require('pino')({ @@ -16,19 +14,6 @@ export class BuffLog { // soon: remove the `v` field https://github.com/pinojs/pino/issues/620 base: {}, - mixin () { - // Check here if a current trace exist to inject it in the log - // `tracer` is a singleton, will no-op if no tracer was initialized - var span = tracer.scope().active() - if (span) { - const traceInfo = {} - tracer.inject(span.context(), formats.LOG, traceInfo); - return traceInfo; - } else { - return {} - } - }, - // notice doesn't exist in pino, let's add it customLevels: { notice: 35 diff --git a/index.ts b/index.ts index 44be4b8..89f27bb 100644 --- a/index.ts +++ b/index.ts @@ -1,10 +1,11 @@ import tracer from "dd-trace"; import express from 'express'; -import bufflog from './bufflog'; +import bufflog from './bufflog'; tracer.init({ hostname: "dd-agent-hostname", - logInjection: false + // will automatically append the traces to BuffLog + logInjection: true }); bufflog.info('hello info');