From 4d838a730c770fa2e80e98aabcdfc4fe7ef0cc60 Mon Sep 17 00:00:00 2001 From: Aneil Mallavarapu Date: Thu, 29 Nov 2018 04:07:23 -0800 Subject: [PATCH] Fixes #332, the EventEmitter memory leak warning The bug was due to reusing transports when creating new loggers. Also upgrades to winston 3.1.0 which fixes a different EventEmitter issue which results in the same warning. --- app-backend/package.json | 2 +- app-backend/src/common/logger.ts | 33 +++++++++++++++++--------------- app-backend/yarn.lock | 21 +++++++++++++++++--- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/app-backend/package.json b/app-backend/package.json index 66c05778..77221aeb 100644 --- a/app-backend/package.json +++ b/app-backend/package.json @@ -102,7 +102,7 @@ "seqvarnomjs": "git+ssh://git@github.com/precisely/seqvarnomjs#1.3.3", "slugify": "^1.3.0", "smart-report": "git+ssh://git@github.com/precisely/smart-report.git#2.0.8", - "winston": "^3.0.0", + "winston": "^3.1.0", "winston-cloudwatch": "^2.0.2" }, "devDependencies": { diff --git a/app-backend/src/common/logger.ts b/app-backend/src/common/logger.ts index c405635b..e073adb3 100644 --- a/app-backend/src/common/logger.ts +++ b/app-backend/src/common/logger.ts @@ -14,23 +14,11 @@ const { format } = winston; const WinstonCloudWatch = require('winston-cloudwatch'); export const LOG_DATA_SEP = '\t|\t'; +import * as fs from 'fs'; type FormatInfo = { timestamp: number, level: number | string, message: string }; const shouldLogToCloudWatchAggregate = process.env.STAGE !== 'prod' && !isOffline; const LOG_LEVEL = (process.env.LOG_LEVEL || 'info').toLowerCase(); -const LOG_TRANSPORTS = shouldLogToCloudWatchAggregate ? [ - new winston.transports.Console(), - - // Adds Cloudwatch logging at - // /precisely/web/{stage}/aggregated-log - new WinstonCloudWatch({ // aggregate view across all lambda fns - level: LOG_LEVEL, - logGroupName: `/precisely/web/${process.env.STAGE}`, - logStreamName: 'aggregated-log' - }) -] : [ - new winston.transports.Console() -]; function makeFormatter(colorize: boolean, requestId?: string) { const plugins = [ format.timestamp(), format.splat() ]; @@ -46,7 +34,6 @@ function makeFormatter(colorize: boolean, requestId?: string) { (info: FormatInfo) => `${info.timestamp} ${info.level}: ${info.message}${LOG_DATA_SEP}`) ); } - return format.combine(... plugins); } @@ -69,10 +56,26 @@ export interface Logger { shouldLog(level: number | string): boolean; } +function makeTransports() { + return shouldLogToCloudWatchAggregate ? [ + new winston.transports.Console(), + + // Adds Cloudwatch logging at + // /precisely/web/{stage}/aggregated-log + new WinstonCloudWatch({ // aggregate view across all lambda fns + level: LOG_LEVEL, + logGroupName: `/precisely/web/${process.env.STAGE}`, + logStreamName: 'aggregated-log' + }) + ] : [ + new winston.transports.Console() + ]; +} + export function makeLogger(requestId?: string): Logger { const shouldColorize = isOffline; const logger = winston.createLogger({ - transports: LOG_TRANSPORTS, + transports: makeTransports(), level: LOG_LEVEL, format: makeFormatter(shouldColorize, requestId) }); diff --git a/app-backend/yarn.lock b/app-backend/yarn.lock index 972bb4ee..dd5a1573 100644 --- a/app-backend/yarn.lock +++ b/app-backend/yarn.lock @@ -1060,7 +1060,7 @@ async-limiter@~1.0.0: async@1.x, async@^1.5.2: version "1.5.2" - resolved "http://registry.npmjs.org/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" + resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" integrity sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo= async@2.5.0: @@ -4714,6 +4714,21 @@ istanbul@^0.4.5: version "0.4.5" resolved "https://registry.yarnpkg.com/istanbul/-/istanbul-0.4.5.tgz#65c7d73d4c4da84d4f3ac310b918fb0b8033733b" integrity sha1-ZcfXPUxNqE1POsMQuRj7C4Azczs= + dependencies: + abbrev "1.0.x" + async "1.x" + escodegen "1.8.x" + esprima "2.7.x" + glob "^5.0.15" + handlebars "^4.0.1" + js-yaml "3.x" + mkdirp "0.5.x" + nopt "3.x" + once "1.x" + resolve "1.1.x" + supports-color "^3.1.0" + which "^1.1.1" + wordwrap "^1.0.0" isurl@^1.0.0-alpha5: version "1.0.0" @@ -6019,7 +6034,7 @@ mixin-deep@^1.2.0: mkdirp@0.5.x, mkdirp@0.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0: version "0.5.1" - resolved "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM= dependencies: minimist "0.0.8" @@ -9224,7 +9239,7 @@ winston-transport@^4.2.0: readable-stream "^2.3.6" triple-beam "^1.2.0" -winston@^3.0.0: +winston@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/winston/-/winston-3.1.0.tgz#80724376aef164e024f316100d5b178d78ac5331" integrity sha512-FsQfEE+8YIEeuZEYhHDk5cILo1HOcWkGwvoidLrDgPog0r4bser1lEIOco2dN9zpDJ1M88hfDgZvxe5z4xNcwg==