1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | 1× 1× 1× 1× 1× 37× 37× 222× 37× 74× 222× 37× 37× 37× 1× 31× 31× 31× 31× 31× 31× 31× 1× 31× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | 'use strict'; const sinon = require('sinon'), devnull = require('dev-null'), fs = require('fs'), path = require('path'), dirname = __dirname.split('/').pop(), lib = require('./'), expect = require('chai').expect, { noop } = require('pino/lib/tools'), semver = require('semver'); describe(dirname, function () { const levels = { trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60 }; let sandbox, fakeLog, pipeSpy, childSpy; function createFakeLogger() { const logger = { child: childSpy, levels: { values: levels } }; Object.keys(levels).forEach((level) => { switch (level) { case 'trace': logger[level] = noop; case 'debug': logger[level] = noop; default: logger[level] = sinon.stub(); } }); const fakeLog = sandbox.stub().returns(logger); fakeLog.pretty = sandbox.stub().returns({ pipe: pipeSpy }); return fakeLog; } beforeEach(function () { process.env.NODE_ENV = 'test'; process.env.CLAY_LOG_PLUGINS = ''; sandbox = sinon.sandbox.create(); pipeSpy = sandbox.spy(); childSpy = sandbox.spy(); fakeLog = createFakeLogger(); lib.setLogger(fakeLog); }); afterEach(function () { sandbox.restore(); }); describe('init', function () { const fn = lib[this.title]; it('throws an error if not instantiated with no arguments', function () { expect(fn).to.throw(Error); }); it('throws an error if not with an empty object', function () { var cb = () => fn({}); expect(cb).to.throw(Error); }); it('throws an error if arg object does not contain a `name` property', function () { var cb = () => fn({ name: 'test' }); expect(cb).to.not.throw(Error); }); it('calls pino.pretty function if `CLAY_LOG_PRETTY` is set to "true"', function () { process.env.CLAY_LOG_PRETTY = 'true'; fn({ name: 'test' }); sinon.assert.calledOnce(fakeLog.pretty); sinon.assert.calledOnce(pipeSpy); delete process.env.CLAY_LOG_PRETTY; }); it('doesn\'t call pino.pretty function if `CLAY_LOG_PRETTY` is set to "false"', function () { process.env.CLAY_LOG_PRETTY = 'false'; fn({ name: 'test'}); sinon.assert.notCalled(fakeLog.pretty); delete process.env.CLAY_LOG_PRETTY; }); it('doesn\'t call pino.pretty function if `CLAY_LOG_PRETTY` is not set', function () { delete process.env.CLAY_LOG_PRETTY; fn({ name: 'test'}); sinon.assert.notCalled(fakeLog.pretty); }); it('calls pino.pretty function if `pretty` is passed into init', function () { fn({ name: 'test', pretty: true }); sinon.assert.calledOnce(fakeLog.pretty); sinon.assert.calledOnce(pipeSpy); }); it('doesn\'t call pino.pretty function if `pretty` is false and CLAY_LOG_PRETTY is "true"', function () { process.env.CLAY_LOG_PRETTY = 'true'; fn({ name: 'test', pretty: false }); sinon.assert.notCalled(fakeLog.pretty); delete process.env.CLAY_LOG_PRETTY; }); it('calls pino.child function if `meta` object is passed in', function () { var fakeMeta = { fake: 'meta' }; fn({ name: 'test', meta: fakeMeta }); sinon.assert.calledWith(childSpy, fakeMeta); sinon.assert.calledOnce(childSpy); }); it('returns a function', function () { expect(fn({ name: 'test'})).is.instanceof(Function); }); }); describe('meta', function () { const fn = lib[this.title]; it('throws an error if no arg is passed in', function () { expect(fn).to.throw(); }); it('calls the logger `child` function to spawn a new logger instance', function () { var fakeMeta = { fake: 'meta' }; fn(fakeMeta, fakeLog()); sinon.assert.calledWith(childSpy, fakeMeta); }); }); describe('log', function () { const fn = lib[this.title]; it('returns a function', function () { expect(fn()).is.instanceof(Function); }); it('calls the level of logging passed in', function () { var fakeLogInstance = { info: sinon.stub() }, log = fn(fakeLogInstance); log('info', 'message'); sinon.assert.calledOnce(fakeLogInstance.info); sinon.assert.calledWith(fakeLogInstance.info, {_label: 'INFO'}, 'message'); }); it('calls the logging level with the arg object', function () { var fakeLogInstance = { info: sinon.stub() }, log = fn(fakeLogInstance), data = { some: 'data' }; log('info', 'message', data); sinon.assert.calledOnce(fakeLogInstance.info); sinon.assert.calledWith(fakeLogInstance.info, data, 'message'); }); it('calls the `error` logging if an error is passed in', function () { var fakeLogInstance = { error: sinon.stub() }, log = fn(fakeLogInstance), fakeError = new Error('issue!'); log(fakeError); sinon.assert.calledOnce(fakeLogInstance.error); sinon.assert.calledWith(fakeLogInstance.error, {_label: 'ERROR'}, fakeError); }); it('logs an error if no level or msg are passed in', function () { var fakeLogInstance = { error: sinon.stub() }, log = fn(fakeLogInstance); log(); sinon.assert.calledOnce(fakeLogInstance.error); }); it('logs memory usage if CLAY_LOG_PLUGINS is set to "heap"', function () { process.env.CLAY_LOG_PLUGINS = 'heap'; const fakeLogInstance = createFakeLogger()(), log = fn(fakeLogInstance), data = { some: 'data' }, expected = { _label: 'INFO', does_zap_garbage: sinon.match.number, heap_size_limit: sinon.match.number, malloced_memory: sinon.match.number, peak_malloced_memory: sinon.match.number, total_available_size: sinon.match.number, total_heap_size: sinon.match.number, total_heap_size_executable: sinon.match.number, total_physical_size: sinon.match.number, used_heap_size: sinon.match.number, some: 'data' }; // Node 12+ includes some additional context. Eif (semver.gte(process.version, '12.0.0')) { expected.number_of_detached_contexts = sinon.match.number; expected.number_of_native_contexts = sinon.match.number; } log('info', 'message', data); sinon.assert.calledOnce(fakeLogInstance.info._original); sinon.assert.calledWith(fakeLogInstance.info._original, expected, 'message'); }); it('doesn\'t log memory usage if CLAY_LOG_PLUGINS does not contain "heap"', function () { process.env.CLAY_LOG_PLUGINS = ''; const fakeLogInstance = createFakeLogger()(), log = fn(fakeLogInstance), data = { some: 'data' }; log('info', 'message', data); sinon.assert.calledOnce(fakeLogInstance.info); sinon.assert.neverCalledWith( fakeLogInstance.info, { used_heap_size: sinon.match.any }, 'message' ); }); it('logs errors to Sentry if CLAY_LOG_PLUGINS is set to "sentry"', function () { process.env.CLAY_LOG_PLUGINS = 'sentry'; const Sentry = require('@sentry/node'); Sentry.captureException = sinon.stub(); const fakeLogInstance = createFakeLogger()(), log = fn(fakeLogInstance), data = { some: 'data' }, expected = { _label: 'ERROR', some: 'data' }; log('error', 'message', data); sinon.assert.calledOnce(fakeLogInstance.error._original); sinon.assert.calledWith(fakeLogInstance.error._original, expected, 'message'); sinon.assert.calledOnce(Sentry.captureException); sinon.assert.calledWith(Sentry.captureException, sinon.match.has('stack'), sinon.match.object); }); it('runs multiple plugins CLAY_LOG_PLUGINS is set to "sentry,heap"', function () { process.env.CLAY_LOG_PLUGINS = 'sentry,heap'; const Sentry = require('@sentry/node'); Sentry.captureException = sinon.stub(); const fakeLogInstance = createFakeLogger()(), log = fn(fakeLogInstance), data = { some: 'data' }; log('error', 'message', data); sinon.assert.calledOnce(fakeLogInstance.error._original._original); sinon.assert.calledWith( fakeLogInstance.error._original._original, sinon.match.has('used_heap_size'), 'message' ); sinon.assert.calledOnce(Sentry.captureException); }); it('skips unavailable or invalid CLAY_LOG_PLUGINS', function () { process.env.CLAY_LOG_PLUGINS = 'foo,bar,baz'; const fakeLogInstance = createFakeLogger()(), log = fn(fakeLogInstance), data = { some: 'data' }, expected = { _label: 'INFO', some: 'data' }; log('info', 'message', data); sinon.assert.calledOnce(fakeLogInstance.info); sinon.assert.calledWith(fakeLogInstance.info, expected, 'message'); }); it('loads custom plugins with CLAY_LOG_PLUGINS_PATH', function (done) { process.env.CLAY_LOG_PLUGINS = 'memory'; fs.mkdtemp(path.join('.', '.clay-log-test-'), (err, dirname) => { Iif (err) throw err; process.env.CLAY_LOG_PLUGINS_PATH = dirname; fs.copyFileSync('./plugins/heap.js', path.join(dirname, 'memory.js')); fs.copyFileSync('./plugins/_utils.js', path.join(dirname, '_utils.js')); const fakeLogInstance = createFakeLogger()(), log = fn(fakeLogInstance), data = { some: 'data' }; log('info', 'message', data); sinon.assert.calledOnce(fakeLogInstance.info._original); sinon.assert.calledWithMatch(fakeLogInstance.info._original, { used_heap_size: sinon.match.any }); fs.unlinkSync(path.join(dirname, 'memory.js')); fs.unlinkSync(path.join(dirname, '_utils.js')); fs.rmdirSync(dirname, { recursive: true }); done(); }); }); }); describe('getLogger', function () { const fn = lib[this.title]; it('returns the logger', function () { var fakeLogger = sandbox.stub().returns('hello'); lib.setLogger(fakeLogger); lib.init({name: 'testing'}); expect(fn()).to.equal('hello'); }); }); describe('wrap', function () { const fn = require('./plugins/_utils')[this.title]; const pino = require('pino'); const logger = pino({ name: 'test-wrapper', level: 'warn' }, devnull()); const fakeService = sinon.stub(); function fakePlugin() { fakeService('foo'); } const wrappedLogger = fn(fakePlugin, ['info', 'warn'])(logger); it('does not trigger plugin due to pino log level being set higher than "info"', function () { wrappedLogger.info('test log: ignore this message'); sinon.assert.notCalled(fakeService); }); it('does not trigger plugin due to plugin config excluding "error"', function () { wrappedLogger.error('test log: ignore this message'); sinon.assert.notCalled(fakeService); }); it('trigers the plugin due to pino log level and plugin including "warn"', function () { wrappedLogger.warn('test log: ignore this message'); sinon.assert.calledOnce(fakeService); }); }); describe('resolvePluginPath', function () { const fn = lib[this.title]; it('resolves an absolute path', function () { expect(fn('/tmp/foo')).to.equal('/tmp/foo'); }); it('resolves a relative path', function () { expect(fn('./tmp/foo')).to.equal(`${process.cwd()}/tmp/foo`); }); it('returns null for an empty string', function () { expect(fn('')).to.equal(null); }); it('returns null for an undefined value', function () { expect(fn(undefined)).to.equal(null); }); }); }); |