Skip to content

Commit

Permalink
better logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonpecora committed May 18, 2017
1 parent 7d39d90 commit d45510f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 48 deletions.
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ const yargs = require('yargs'),
logger = require('./lib/utils/logger'),
options = require('./lib/utils/shared-options');

let argv = yargs.usage('Usage: clay <command> [options]')
let argv = yargs
.usage('Usage: clay <command> [options]')
.wrap(yargs.terminalWidth())
// commands
.commandDir(path.join('lib', 'cmd'))
.option('V', options.verbose).argv;

// set log level before instantiating commands
logger.init(argv.verbose);

yargs.commandDir(path.join('lib', 'cmd'))
// common options
.help()
.version()
.alias({
h: 'help',
v: 'version'
})
.option('V', options.verbose)
.demandCommand(1, 'What would you like to do today?')
.argv;

// set log level (note: this has to be set in your command handlers if you want to debug stuff there)
logger.setLogLevel(argv.verbose);
6 changes: 3 additions & 3 deletions lib/cmd/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function handler(argv) {
let val = config.get(argv.alias);

if (val) {
logger.log(val);
logger.info(val);
} else {
logger.warn(`No value defined for "${argv.alias}"`);
}
Expand All @@ -27,9 +27,9 @@ function handler(argv) {
// set value in .clayconfig
try {
config.set(argv.alias, argv.value);
logger.log(`Saved ${argv.alias.split('.')[0]} ${argv.alias.split('.')[1]} = ${argv.value}`);
logger.info(`Saved ${argv.alias.split('.')[0]} ${argv.alias.split('.')[1]} = ${argv.value}`);
} catch (e) {
logger.fatal(e.message);
logger.error(e.message);
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions lib/cmd/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ function getInstances(instances, name) {
return rest.get(config.normalizeSite(uri));
})).then(() => {
logger.stopSpinner(processSpinner);
logger.log(`GOT ${instances.length} instances of ${name}!`);
logger.success(`GOT ${instances.length} instances of ${name}!`);
});
}

function runLogic(prefix, name, argv) {
return clay.getComponentInstances(prefix, name).then((instances) => {
if (argv.n) {
// dry run
logger.log(`This would run GET requests against ${instances.length} instances of ${name}`);
logger.info(`This would run GET requests against ${instances.length} instances of ${name}`);
} else if (argv.force) {
// force
return getInstances(instances, name);
Expand Down Expand Up @@ -55,7 +55,6 @@ function builder(yargs) {
}

function handler(argv) {
logger.setLogLevel(argv.verbose);
if (clayUtils.isComponent(argv.component)) {
// component url passed in, get the number of instances and tell the user
let url = config.normalizeSite(argv.component), // apply all the site normalization stuff
Expand All @@ -75,7 +74,7 @@ function handler(argv) {
// will help you diagnose that issue
return runLogic(prefix, name, argv);
} else {
logger.fatal('Please provide component uri OR component name and site!');
logger.error('Please provide component uri OR component name and site!');
}
}

Expand Down
63 changes: 30 additions & 33 deletions lib/utils/logger.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
const davlog = require('davlog'),
const consoleLog = require('console-log-level'),
_ = require('lodash'),
ora = require('ora'),
logger = davlog.init({
name: 'clay'
}),
spinners = {};
figures = require('figures'),
chalk = require('chalk'),
spinners = {},
prefixes = {
debug: chalk.dim(chalk.magenta('[DEBUG]')),
info: chalk.blue('[INFO]'),
success: chalk.green(` ${figures.tick}`),
warn: chalk.yellow('[WARNING]'),
error: chalk.red('[ERROR]')
};

// use logger.log as the debug level
logger.STRINGS.log = 'debug';
// fatal errors exit the application
logger.STRINGS.error = 'fatal';
// regular errors don't exit the application
logger.STRINGS.err = 'error';
let logger;

let logLevel = 'INFO'; // default log level

/**
* set log level based on verbosity
* @param {Boolean} isVerbose true if -V, --verbose
*/
function setLogLevel(isVerbose) {
if (isVerbose) {
logLevel = 'DEBUG';
logger = consoleLog({ level: 'debug' });
} else {
logLevel = 'INFO';
logger = consoleLog({ level: 'info' });
}
}

/**
* only log debug messages in verbose mode
* @param {string} message
*/
function logDebug(message) {
if (logLevel === 'DEBUG') {
logger.log(message);
}
function log(level) {
return (message) => {
const method = level === 'success' ? 'info' : level;

if (!logger) {
throw new Error('Please call logger.init() to initialize a logger!');
}

logger[method](`${prefixes[level]} ${message}`);
};
}

/**
Expand Down Expand Up @@ -67,12 +64,12 @@ function stopSpinner(spinner) {
}

module.exports = {
setLogLevel,
debug: logDebug,
log: logger.info,
init: setLogLevel,
debug: log('debug'),
info: log('info'),
success: log('success'),
startSpinner,
stopSpinner,
warn: logger.warn,
error: logger.err,
fatal: logger.error
warn: log('warn'),
error: log('error')
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
"bluebird": "^3.5.0",
"chalk": "^1.1.3",
"clay-utils": "^1.1.1",
"davlog": "^1.1.0",
"console-log-level": "^1.4.0",
"expand-home-dir": "0.0.3",
"figures": "^2.0.0",
"home-config": "^0.1.0",
"inquirer": "^3.0.6",
"lodash": "^4.17.4",
Expand Down

0 comments on commit d45510f

Please sign in to comment.