From 2c4d4713f2d7439d12d76619932879cb6df0f668 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 31 Oct 2012 22:46:05 -0700 Subject: [PATCH] issue #48: make dtrace-provider presence optional (disabled dtrace probes) dtrace-provider is still in *dependencies* to strongly encourage installing it. However I want bunyan.js to be useable for basic usage standalone. --- lib/bunyan.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/bunyan.js b/lib/bunyan.js index bbb2984..4a0d6ec 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -25,7 +25,11 @@ var os = require('os'); var fs = require('fs'); var util = require('util'); var assert = require('assert'); -var dtrace = require('dtrace-provider'); +try { + var dtrace = require('dtrace-provider'); +} catch (e) { + dtrace = null; +} var EventEmitter = require('events').EventEmitter; @@ -48,7 +52,7 @@ function objCopy(obj) { var format = util.format; if (!format) { - // If not node 0.6, then use its `util.format`: + // If node < 0.6, then use its `util.format`: // : var inspect = util.inspect; var formatRegExp = /%[sdj%]/g; @@ -157,8 +161,9 @@ var levelFromName = { 'fatal': FATAL }; +// Dtrace probes. var dtp = undefined; -var probes = {}; +var probes = dtrace && {}; /** * Resolve a level number, name (upper or lowercase) to a level number value. @@ -302,7 +307,7 @@ function Logger(options, _childOptions, _childSimple) { this.fields = {}; } - if (!dtp) { + if (!dtp && dtrace) { dtp = dtrace.createDTraceProvider('bunyan'); for (var level in levelFromName) { @@ -768,7 +773,7 @@ function mkLogEmitter(minLevel) { rec = mkRecord(msgArgs); str = this._emit(rec); } - probes[minLevel].fire(function () { + probes && probes[minLevel].fire(function () { return [ str || (rec && log._emit(rec, true)) || log._emit(mkRecord(msgArgs), true) ];