Closed Zolmeister closed 8 years ago
Bump
Sorry, I've been crazy busy. Back on this now!
I'm still not keen to merge this though I'm afraid - it's a feature that defeats a substantial part of the point of this library (a reliably working console that doesn't break anywhere - including always not breaking stacktraces), increases the complexity of the codebase and doesn't feel like something that really fits here, sorry.
I do have good news though! I've added a plugin API that enables exactly this use case, and means you can directly implement this yourself as a plugin on top of loglevel, while loglevel stays small and focused. This is currently in master, and I'll be doing a 1.2 release with it within the next few days or so, once I've done some more thorough cross-browser testing.
This API lets you set a log.methodFactory function, which gets called to provide a logging function for each enabled method, and given the name of the method, and the currently set level.
In your case, this should look something like:
<script src="loglevel.js"></script>
<script> // Plugin source:
(function () {
var originalFactory = log.methodFactory;
var eventListeners = {};
log.methodFactory = function (methodName, logLevel) {
var rawMethod = originalFactory(methodName, logLevel);
return function () {
var listeners = eventListeners[methodName];
var length = listeners ? listeners.length : 0;
for (var ii = 0; ii < length; ii++) {
listeners[ii].apply(null, arguments);
}
};
};
log.on = function (method, fn) {
eventListeners[method] = eventListeners[method] || [];
eventListeners[method].push(fn);
};
log.off = function (method, fn) {
if (!fn) {
eventListeners[method] = [];
return;
}
var listeners = eventListeners[method];
var index = listeners ? listeners.indexOf(fn) : -1;
if (index !== -1) {
eventListeners[method].splice(index, 1);
}
};
})();
</script>
(approximately, just cribbing from your changes here)
Does this solve your problem? That should add the features you're looking for, while still giving you the cross-browser handling etc from loglevel underneath. If you've got any time it'd be really great for me if you could try this out and give me any thoughts you did have on this, or on the API as an extension point generally. No worries if you're busy though.
If you're feeling particularly keen, I think the best solution is for you to take and tweak and publish this as a plugin yourself, and then I'm happy to drop a link into the README here for anybody else looking to do the same thing.
How does all that sound?
Ah, and alternatively of course you could actually make this less generic, since it'd be standalone anyway. Rather than on(method, fn)
and off(method, fn)
, you can just build a plugin adding a loglevel.postMessagesTo(url)
method. Nice.
Been quiet for a long while now, so closing this. Feel free to bring this up to date and reopen it if you're still interested.
Ended up re-writing the lib for my use-case: https://github.com/zolmeister/loga
As per #52, the listeners are conditional. If they are bound, then
console
will be wrapped. Otherwise it stays the same. This creates the best experience for separate development and production configurations.