From 18606613d69175ac86a1da39b10a03ac1f0f13f0 Mon Sep 17 00:00:00 2001 From: Tavis Rudd Date: Mon, 28 Aug 2017 16:08:30 -0700 Subject: [PATCH] add --debug flag and some refactoring related to that --- src/cli.ts | 12 ++++++++++-- src/debug.ts | 2 +- src/logger.ts | 24 ++++++++++++++++++------ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index c0fccfae..2215030b 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -2,7 +2,7 @@ import * as process from 'process'; import * as yargs from 'yargs'; import * as cli from 'cli-color'; -import { logger } from './logger'; +import { logger, setLogLevel } from './logger'; import debug from './debug'; type ExitCode = number; @@ -11,8 +11,12 @@ type Handler = (args:yargs.Arguments) => Promise const wrapMainHandler = (handler: Handler) => // move the configureaws step into here function (args: yargs.Arguments) { + if (args.debug) { + process.env.DEBUG = 'true'; + setLogLevel('debug'); + } handler(args) - .then(exitCode => process.exit(exitCode)) + .then(process.exit) .catch(error => { if (error.message) { logger.error(error.message); @@ -279,6 +283,10 @@ export async function main() { group: 'AWS Options', description: 'AWS profile'}) + .option('debug', { + type: 'boolean', default: false, + description: 'Log debug information to stderr.'}) + .demandCommand(1) .usage(usage) .alias('v', 'version') diff --git a/src/debug.ts b/src/debug.ts index 3905446b..6b999a93 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -1,4 +1,4 @@ function debug(): boolean { - return typeof process.env.DEBUG === 'string' || process.env.LOG_LEVEL === 'DEBUG'; + return process.env.LOG_LEVEL === 'DEBUG' || process.env.DEBUG !== undefined; } export default debug; diff --git a/src/logger.ts b/src/logger.ts index e2c916c6..5854f59e 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,13 +1,25 @@ import * as process from 'process'; import * as _ from 'lodash'; import * as winston from 'winston'; +import debug from './debug'; -export const logger = new winston.Logger({ - transports: [ - new winston.transports.Console({ +let LOG_LEVEL: string; +if (debug()) { + LOG_LEVEL = 'debug'; +} else { + LOG_LEVEL = _.get(process.env, 'LOG_LEVEL', 'info').toLowerCase(); +} + +const consoleLogger = new winston.transports.Console({ colorize: true, - level: _.get(process.env, 'LOG_LEVEL', 'info'), + level: LOG_LEVEL, prettyPrint: true - }) - ] +}); + +export function setLogLevel(level: string) { + consoleLogger.level = level; +} + +export const logger = new winston.Logger({ + transports: [consoleLogger] });