Created
November 22, 2014 08:25
-
-
Save alessioalex/ab84c2daef8ae49ad49c to your computer and use it in GitHub Desktop.
configurable-logger-example.js
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
// TODO: given the log function previously created, create a prefixed logger | |
// function that takes a {String} as an argument. | |
// That string should be used as the first argument to the regular `log` function. | |
var log = function log() { | |
var args = Array.prototype.slice.call(arguments); | |
console.log(args.join(' ')); | |
}; | |
// log('hello', 'world', '!'); | |
// -> Hello world ! | |
function createLogger(prefix) { | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
args.unshift(prefix); | |
// second argument === Array | |
log.apply(null, args); | |
}; | |
} | |
// Example: | |
var logger = createLogger('MyApp ::'); | |
logger('hello', 'world', '!'); | |
// -> MyApp :: Hello world ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment