-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow logging function overrides & add more loggers #4
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
export { default } from 'ember-debug-logger/utils/debug-logger'; | ||
import { | ||
overrideLoggers, | ||
debugLogger, | ||
infoLogger, | ||
warnLogger, | ||
errorLogger | ||
} from 'ember-debug-logger/utils/debug-logger'; | ||
|
||
export { | ||
overrideLoggers, | ||
debugLogger, | ||
infoLogger, | ||
warnLogger, | ||
errorLogger | ||
}; | ||
|
||
export default debugLogger; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,25 +13,90 @@ | |
* Logging preferences are persisted in local storage, and you'll need to reload | ||
* the page for changes to take effect. | ||
*/ | ||
export default function debugLogger(key) { | ||
return key ? window.debug(key) : instanceLogger; | ||
|
||
/** | ||
* The default logging functions | ||
* @type {{debug: *, info: *, warn: *, error: *}} | ||
*/ | ||
let LOGGERS = { | ||
'debug': console.log.bind(console), | ||
'info' : console.info.bind(console), | ||
'warn' : console.warn.bind(console), | ||
'error': console.error.bind(console) | ||
}; | ||
|
||
/** | ||
* These functions wrap the loggers, so we can perform runtime lookups | ||
* of the loggers, and change them on the fly. | ||
* @type {{debug: (function(): *), info: (function(): *), warn: (function(): *), error: (function(): *)}} | ||
*/ | ||
const WRAPPERS = { | ||
'debug': (...args) => LOGGERS['debug'](...args), | ||
'info' : (...args) => LOGGERS['info'](...args), | ||
'warn' : (...args) => LOGGERS['warn'](...args), | ||
'error': (...args) => LOGGERS['error'](...args) | ||
}; | ||
|
||
/** | ||
* Replaces the logging function for the various log types. | ||
* Expects an object with keys of: debug, info, warn, or error | ||
* The values should be a function. | ||
* | ||
* @function overrideLoggers | ||
* @param {Object} loggers a hash as described above | ||
*/ | ||
export function overrideLoggers(loggers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I follow the logic here — it looks like you're setting the global |
||
LOGGERS = loggers; | ||
for (const type of Object.keys(loggers)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When Babel compiles a |
||
LOGGERS[type] = loggers[type]; | ||
} | ||
} | ||
|
||
const LOGGER = '_debugLoggerInstance'; | ||
/** | ||
* Creates an instance logger of type, type | ||
* @param type | ||
* @returns {Function} | ||
*/ | ||
function createInstanceLogger(type) { | ||
return function () { | ||
let logger = this && this[`${type}InstanceLogger`]; | ||
|
||
if (!logger) { | ||
const loggerKey = this && this._debugContainerKey; | ||
if (!loggerKey) { | ||
throw new Error('On non-container-managed objects, debug-logger requires an explicit key.'); | ||
} | ||
|
||
export function instanceLogger() { | ||
let logger = this && this[LOGGER]; | ||
logger = window.debug(loggerKey); | ||
logger.log = WRAPPERS[type]; | ||
|
||
if (!logger) { | ||
const loggerKey = this && this._debugContainerKey; | ||
if (!loggerKey) { | ||
throw new Error('On non-container-managed objects, debug-logger requires an explicit key.'); | ||
Object.defineProperty(this, type, { value: logger }); | ||
} | ||
|
||
logger = window.debug(loggerKey); | ||
return logger.apply(this, arguments); | ||
}; | ||
} | ||
|
||
const instanceDebugLogger = createInstanceLogger('debug'); | ||
const instanceInfoLogger = createInstanceLogger('info'); | ||
const instanceWarnLogger = createInstanceLogger('warn'); | ||
const instanceErrorLogger = createInstanceLogger('error'); | ||
|
||
Object.defineProperty(this, LOGGER, { value: logger }); | ||
} | ||
export function debugLogger(key) { | ||
return key ? window.debug(key) : instanceDebugLogger; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
} | ||
|
||
return logger.apply(this, arguments); | ||
|
||
export function infoLogger() { | ||
return instanceInfoLogger; | ||
} | ||
|
||
export function warnLogger() { | ||
return instanceWarnLogger; | ||
} | ||
|
||
export function errorLogger() { | ||
return instanceErrorLogger; | ||
} | ||
|
||
export default debugLogger; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure these type annotations are providing much use.