UNPKG

@apartmentlist/js-trace-logger

Version:
47 lines (46 loc) 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogFormatter = void 0; const dd_trace_1 = require("dd-trace"); const util_1 = require("./util"); const constant_1 = require("./constant"); class LogFormatter { constructor(option, passed_tracer) { const { env, service, version, progname, logTemplate, traceTemplate, dateFunc } = option; this.env = env; this.service = service; this.version = version; this.progname = progname; this.logFunc = (0, util_1.compileTemplate)(logTemplate); this.traceFunc = (0, util_1.compileTemplate)(traceTemplate); this.dateFunc = dateFunc; this.tracer = passed_tracer || dd_trace_1.tracer; } format(dt, sev, msg) { return this.logFunc({ datetime: this.dateFunc(dt), progname: this.progname, severity: constant_1.LoggerSeverity[sev], trace: this.toTraceString(), msg: msg, }); } toTraceString() { const activeSpan = this.tracer.scope().active(); let trace_id = '1'; let span_id = '1'; if (activeSpan) { const context = activeSpan.context(); trace_id = context.toTraceId(); span_id = context.toSpanId(); } return this.traceFunc({ env: this.env, service: this.service, version: this.version, trace_id: trace_id, span_id: span_id, }); } } exports.LogFormatter = LogFormatter;