This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable log levels for builder itself.
* Refactor `builder.js` to pass a buffer of messages to builder to be later logged. * Refactor to queue up log statements until `log.setLevel()` is called. The issue we're solving is logging in "./environment" when environment is needed to determine what level to log at. * Adds `--log-level=info|warn|error|none` flag. Fixes #6 * Adds `--quiet` flag as alias for `--log-level=none`. * Adds env variable `_BUILDER_ARGS_LOG_LEVEL=info|warn|error|none` to also control. * Add tests and docs.
- Loading branch information
1 parent
6988513
commit 2a7ec90
Showing
10 changed files
with
237 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,105 @@ | ||
"use strict"; | ||
|
||
var chalk = require("chalk"); | ||
var args = require("./args"); | ||
|
||
// Wrap "type". | ||
var wrapType = function (type) { | ||
return "[builder" + (type ? ":" + type : "") + "]"; | ||
}; | ||
|
||
// Levels | ||
var LEVELS = { | ||
info: 0, | ||
warn: 1, | ||
error: 2, | ||
none: 3 | ||
}; | ||
|
||
/** | ||
* A super-small logger. | ||
*/ | ||
module.exports = { | ||
// TODO(6): Configurable log levels. | ||
// https://github.com/FormidableLabs/builder/issues/6 | ||
_logger: function () { | ||
return console; | ||
var log = module.exports = { | ||
/** | ||
* Set log level from command flags or environment. | ||
* | ||
* **NOTE - Side Effects**: Mutates environment. | ||
* | ||
* @param {Object} opts Options object | ||
* @param {Object} opts.argv Arguments array. | ||
* @param {Object} opts.env Environment object to mutate (Default `process.env`) | ||
* @returns {void} | ||
*/ | ||
setLevel: function (opts) { | ||
opts = opts || {}; | ||
var env = opts.env && opts.env.env || process.env; | ||
|
||
// Try to determine log level from environment. | ||
var level = env._BUILDER_ARGS_LOG_LEVEL; | ||
|
||
// If not, determine log level from command line. | ||
if (!level) { | ||
var parsed = args.general(opts.argv); | ||
level = parsed.quiet === true ? "none" : parsed.logLevel; | ||
} | ||
|
||
// Statefully set level. | ||
env._BUILDER_ARGS_LOG_LEVEL = level; | ||
log._level = LEVELS[level]; | ||
if (typeof log._level === "undefined") { | ||
throw new Error("Unknown log level: " + level); | ||
} | ||
|
||
// Drain message queue. | ||
log._drainQueue(); | ||
}, | ||
|
||
info: function (type, msg) { | ||
this._logger().info([chalk.green(wrapType(type)), msg].join(" ")); | ||
// Nuke everything for test runs. | ||
_unsetLevel: function () { | ||
delete process.env._BUILDER_ARGS_LOG_LEVEL; | ||
delete log._level; | ||
delete log._queue; | ||
}, | ||
|
||
warn: function (type, msg) { | ||
this._logger().warn([chalk.yellow(wrapType(type)), msg].join(" ")); | ||
// Drain internal queue and emit log events. | ||
_drainQueue: function () { | ||
(log._queue || []).forEach(function (obj) { | ||
log[obj.level](obj.type, obj.msg); | ||
}); | ||
log._queue = null; | ||
}, | ||
|
||
error: function (type, msg) { | ||
this._logger().error([chalk.red(wrapType(type)), msg].join(" ")); | ||
_logger: function () { | ||
return console; | ||
}, | ||
|
||
_wrapper: function (level, color, type, msg) { // eslint-disable-line max-params | ||
// Queue if level is unset. | ||
// | ||
// **Scenario**: This queue is used when `log` has been imported from the | ||
// actual builder instance that is running. However, we have calls to log | ||
// events before we can call `log.setLevel`, so need to queue until that | ||
// happens. | ||
if (typeof log._level === "undefined") { | ||
log._queue = log._queue || []; | ||
log._queue.push({ level: level, type: type, msg: msg }); | ||
return; | ||
} | ||
|
||
// Should only get to here, _after_ `log.setlevel()` is imported. | ||
// Check if logging at this integer level. | ||
if (LEVELS[level] < log._level) { | ||
return; | ||
} | ||
|
||
// Call directly once level is set. | ||
// This is also used to drain the queue. | ||
var formattedMsg = [chalk[color](wrapType(type)), msg].join(" "); | ||
log._logger()[level](formattedMsg); | ||
} | ||
}; | ||
|
||
// Actual implementation methods. | ||
log.info = log._wrapper.bind(log, "info", "green"); | ||
log.warn = log._wrapper.bind(log, "warn", "yellow"); | ||
log.error = log._wrapper.bind(log, "error", "red"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.