From 584111ee4399bd52c51b65c075051c9efddb0184 Mon Sep 17 00:00:00 2001 From: HonzaMac Date: Thu, 17 Jun 2021 09:25:27 +0200 Subject: [PATCH] Fix: Missing defaultMeta in log entry while using Profiler fixes #1665 --- lib/winston/profiler.js | 8 ++++++++ test/helpers/index.js | 3 ++- test/logger.test.js | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index edcc5a65a..f73289a04 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -45,7 +45,15 @@ module.exports = class Profiler { const info = typeof args[args.length - 1] === 'object' ? args.pop() : {}; info.level = info.level || 'info'; info.durationMs = (Date.now()) - this.start; + this._addDefaultMeta(info); return this.logger.write(info); } + + _addDefaultMeta(msg) { + if (this.logger.defaultMeta) { + Object.assign(msg, this.logger.defaultMeta); + } + } + }; diff --git a/test/helpers/index.js b/test/helpers/index.js index fe51be9e3..06801a894 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -25,9 +25,10 @@ var helpers = exports; * @param {function} write Write function for the specified stream * @returns {Logger} A winston.Logger instance */ -helpers.createLogger = function (write, format) { +helpers.createLogger = function (write, format, defaultMeta) { return winston.createLogger({ format, + defaultMeta, transports: [ mockTransport.createMockTransport(write) ] diff --git a/test/logger.test.js b/test/logger.test.js index 0d21d3fa1..c288260fc 100755 --- a/test/logger.test.js +++ b/test/logger.test.js @@ -905,9 +905,10 @@ describe('Logger (profile, startTimer)', function (done) { assume(info.level).equals('info'); assume(info.durationMs).is.a('number'); assume(info.message).equals('testing1'); + assume(info.service).equals('your-service-name'); assume(info[MESSAGE]).is.a('string'); done(); - }); + }, null, { service: 'your-service-name' }); var timer = logger.startTimer(); setTimeout(function () {