-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a logging mechanism that can be dissabled in production (#103)
* chore: Add 'preprocess' in gulp It allows to change JS/HTML/... files depending on environment variables. It is similar to macros in C where you can inject code if some environment variables are set. Docs: https://www.npmjs.com/package/gulp-preprocess The preprocess will be used to enabled/disable a the logger according to the development and production modes. See #102 * feat(logger): Add a toggling logger mechanism for dev/prod environments
- Loading branch information
Showing
5 changed files
with
107 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use strict"; | ||
|
||
class Logger { | ||
static isEnabled = false; | ||
|
||
/** | ||
* Generic function that calls one of the 'console' functions (e.g. 'debug') | ||
* if the `isEnabled` attribute is set. Moreover, it adds a styled prefix | ||
* to the message saying "SigTools" :) | ||
* | ||
* @param {Function} fn A console function reference, e.g. | ||
* `console.debug` | ||
* @param {any[]} args The arguments list to be passed to the console | ||
* function | ||
*/ | ||
static _caller(fn, args) { | ||
Logger.isEnabled && fn( | ||
"%cSigTools", | ||
"font-weight: bold; background-color: #e74c3c; color: white; padding: 0.5em 0.25em", | ||
...args | ||
); | ||
} | ||
|
||
// A list of functions available in 'console' and supported in our logger | ||
// This is just a declaration because of the IDEs. The definition is below | ||
// See: https://developer.mozilla.org/en-US/docs/Web/API/console | ||
static debug() { } | ||
static error() { } | ||
static info() { } | ||
static log() { } | ||
static warn() { } | ||
} | ||
|
||
// dynamically define the interfaces in our logger to existing functions in | ||
// 'console' | ||
if (console) { | ||
|
||
// get the static methods added above, e.g. 'debug', 'error', etc | ||
const allFn = Object.getOwnPropertyNames(Logger).filter( | ||
prop => typeof Logger[prop] === "function" && prop !== '_caller' | ||
); | ||
|
||
for (const fnName of allFn) { | ||
Logger[fnName] = function () { | ||
Logger._caller(console[fnName], arguments); | ||
} | ||
} | ||
} | ||
|
||
// @if DEBUG=true | ||
// logger is only enabled when 'gulp' env is set to development mode | ||
Logger.isEnabled = true; | ||
// @endif |