Skip to content

Commit

Permalink
add --debug flag and some refactoring related to that
Browse files Browse the repository at this point in the history
  • Loading branch information
tavisrudd committed Aug 28, 2017
1 parent ada8af6 commit 1860661
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,8 +11,12 @@ type Handler = (args:yargs.Arguments) => Promise<ExitCode>
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);
Expand Down Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/debug.ts
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 18 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -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]
});

0 comments on commit 1860661

Please sign in to comment.