From 245f64312f84e5c7f35ae293e5f936a3d25a8d94 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Sat, 14 Jan 2017 13:50:39 +0100
Subject: [PATCH] Refactor logger

Avoid passing it around between modules
---
 app/config/parser.js         | 17 +++--------------
 app/helpers/github.js        |  5 ++---
 app/helpers/npm.js           |  8 +-------
 app/helpers/package-json.js  |  7 +------
 app/index.js                 |  6 ++++--
 app/logger.js                | 13 +++++++++++++
 app/renovate.js              |  7 ++-----
 test/helpers/npm.js          |  2 --
 test/helpers/package-json.js |  3 ---
 9 files changed, 26 insertions(+), 42 deletions(-)
 create mode 100644 app/logger.js

diff --git a/app/config/parser.js b/app/config/parser.js
index c35181ebb7..63b08a1565 100644
--- a/app/config/parser.js
+++ b/app/config/parser.js
@@ -1,15 +1,6 @@
-const winston = require('winston');
+const logger = require('winston');
 const program = require('commander');
 
-// Add the logger
-const logger = new (winston.Logger)({
-  transports: [
-    // colorize the output to the console
-    new (winston.transports.Console)({ colorize: true }),
-  ],
-});
-logger.level = process.env.LOG_LEVEL || 'info';
-
 module.exports = {
   getGlobalConfig,
   getCascadedConfig,
@@ -91,8 +82,9 @@ function getGlobalConfig() {
     cliConfig.token = program.token;
   }
 
-  // Set global config
+  // Get global config
   config = Object.assign({}, defaultConfig, customConfig, envConfig, cliConfig);
+
   // Set log level
   logger.level = config.logLevel;
 
@@ -132,9 +124,6 @@ function getGlobalConfig() {
   // Print config
   logger.verbose(`config=${JSON.stringify(config)}`);
 
-  // Add logger to config
-  config.logger = logger;
-
   return config;
 }
 
diff --git a/app/helpers/github.js b/app/helpers/github.js
index 5918de4e7c..b88c510aa1 100644
--- a/app/helpers/github.js
+++ b/app/helpers/github.js
@@ -1,7 +1,7 @@
+const logger = require('winston');
 const ghGot = require('gh-got');
 
 const config = {};
-let logger = null;
 
 module.exports = {
   init,
@@ -21,9 +21,8 @@ module.exports = {
   updatePr,
 };
 
-function init(token, l) {
+function init(token) {
   config.token = token;
-  logger = l;
 }
 
 // Initialize GitHub by getting base branch and SHA
diff --git a/app/helpers/npm.js b/app/helpers/npm.js
index 9798c211a7..14a6ed26b5 100644
--- a/app/helpers/npm.js
+++ b/app/helpers/npm.js
@@ -1,11 +1,9 @@
+const logger = require('winston');
 const got = require('got');
 const semver = require('semver');
 const stable = require('semver-stable');
 
-let logger = null;
-
 module.exports = {
-  setLogger,
   extractDependencies,
   findUpgrades,
   getUpgrades,
@@ -13,10 +11,6 @@ module.exports = {
   isValidVersion,
 };
 
-function setLogger(l) {
-  logger = l;
-}
-
 // Returns an array of current dependencies
 function extractDependencies(packageJson, sections) {
   // loop through dependency types
diff --git a/app/helpers/package-json.js b/app/helpers/package-json.js
index 680fb10d8f..246e2a36eb 100644
--- a/app/helpers/package-json.js
+++ b/app/helpers/package-json.js
@@ -1,16 +1,11 @@
+const logger = require('winston');
 const _ = require('lodash');
 
-let logger = null;
 
 module.exports = {
-  setLogger,
   setNewValue,
 };
 
-function setLogger(l) {
-  logger = l;
-}
-
 function setNewValue(currentFileContent, depType, depName, newVersion) {
   logger.debug(`setNewValue: ${depType}.${depName} = ${newVersion}`);
   const parsedContents = JSON.parse(currentFileContent);
diff --git a/app/index.js b/app/index.js
index 693ec73ab6..a3b31731a0 100644
--- a/app/index.js
+++ b/app/index.js
@@ -1,5 +1,7 @@
 #!/usr/bin/env node
 
+const logger = require('./logger');
+
 // Initialize config
 const configParser = require('./config/parser');
 // Require main source
@@ -19,8 +21,8 @@ config.repositories.forEach((repo) => {
   });
 });
 p.then(() => { // eslint-disable-line promise/always-return
-  config.logger.info('Renovate finished');
+  logger.info('Renovate finished');
 })
 .catch((error) => {
-  config.logger.error(`Unexpected error: ${error}`);
+  logger.error(`Unexpected error: ${error}`);
 });
diff --git a/app/logger.js b/app/logger.js
new file mode 100644
index 0000000000..5097e1a41d
--- /dev/null
+++ b/app/logger.js
@@ -0,0 +1,13 @@
+const logger = require('winston');
+
+// Set log level from environment. It may be overriden later by configuration
+logger.level = process.env.LOG_LEVEL || 'info';
+
+// Colorize console logs
+logger.configure({
+  transports: [
+    new (logger.transports.Console)({ colorize: true }),
+  ],
+});
+
+module.exports = logger;
diff --git a/app/renovate.js b/app/renovate.js
index d59eec0ca2..8dc6e3d9dd 100644
--- a/app/renovate.js
+++ b/app/renovate.js
@@ -1,23 +1,20 @@
+const logger = require('winston');
 const changelog = require('changelog');
 const github = require('./helpers/github');
 const npm = require('./helpers/npm');
 const packageJson = require('./helpers/package-json');
 
 let config = null;
-let logger = null;
 
 module.exports = renovate;
 
 // This function manages the queue per-package file
 function renovate(repoName, packageFile, setConfig) {
   // Initialize globals
-  logger = setConfig.logger;
   config = Object.assign({}, setConfig);
 
   // Initialize helpers
-  github.init(config.token, logger);
-  npm.setLogger(logger);
-  packageJson.setLogger(logger);
+  github.init(config.token);
   changelog.setGitHubToken(config.token);
 
   logger.info(`Processing ${repoName} ${packageFile}`);
diff --git a/test/helpers/npm.js b/test/helpers/npm.js
index 3d2f7b63e2..3d9274b49d 100644
--- a/test/helpers/npm.js
+++ b/test/helpers/npm.js
@@ -7,8 +7,6 @@ const defaultTypes = ['dependencies', 'devDependencies'];
 
 chai.should();
 
-npm.setLogger(winston);
-
 const input01Content = fs.readFileSync('./test/_fixtures/package.json/inputs/01.json', 'utf8');
 const input02Content = fs.readFileSync('./test/_fixtures/package.json/inputs/02.json', 'utf8');
 
diff --git a/test/helpers/package-json.js b/test/helpers/package-json.js
index 0a27c47178..689c041d7e 100644
--- a/test/helpers/package-json.js
+++ b/test/helpers/package-json.js
@@ -1,9 +1,6 @@
 const expect = require('chai').expect;
 const fs = require('fs');
 const packageJson = require('../../app/helpers/package-json');
-const winston = require('winston');
-
-packageJson.setLogger(winston);
 
 const inputContent = fs.readFileSync('./test/_fixtures/package.json/inputs/01.json', 'utf8');
 
-- 
GitLab