Closed nopmop closed 8 years ago
Yes, the console methods you end up with will have the signature of the function that gets returned here (i.e. in this case they'll only accept one argument). You can easily make them accept more, but how exactly you do that depends on what you're trying to do.
If for example you just want to add an extra string separately at the start, then something like the below should work:
var originalFactory = log.methodFactory;
log.methodFactory = function (methodName, logLevel, loggerName) {
var rawMethod = originalFactory(methodName, logLevel, loggerName);
return function () {
var argsArray = Array.prototype.slice.call(arguments);
argsArray.unshift("Newsflash: ");
rawMethod.apply(this, argsArray);
};
};
Does that make sense? Hopefully that gives you a broad idea of what to look at for this, and I'm happy to help more if you can give me the exact details of what you're trying to do.
@pimterry Hello Perry, I was actually just trying to get all the arguments printed on the console - because as I said in the example message was carrying only the first argument. I ended up making it too in the same way you did, and I realized that loglevel has a little shortcoming (or a feature perhaps) which is essential to me: logging objects in a non-stringify'ed form on console (ex: collapsible/expandable arrays etc.). I noticed that loglevel stringifyes everything.
Because I was in a rush to plug a logging tool into my app i ended up switching to another logging project called "minilog" (https://github.com/mixu/minilog) - it's built on streams and the way in which it works is pretty amazing, it's all pipes (never seen anything like this in JS before. I'll have to study it).
Anyways, thanks a lot for your support, you have been very kind. Wish you success in your projects :+1:
Loglevel definitely isn't stringifying messages in this case, that must be something else going on. If you look at the above code at runtime you'll see that rawMethod is actually just the bound built-in console method (for enabled levels). Once you've added a plugin then within that plugin there should be nothing else going on between your code and the built-in console APIs.
Interesting anyway, I'll take a look at minilog, thanks! Best of luck, I'll close this now, but feel free to reopen this (you or anybody else) if there's more on this issue.
Hello!
I have tried writing a plugin as the Documentation says:
however, message does not carry all the arguments passed to log.debug(), but only the 1st one.
For example using the console.debug() one would do this...
...and all the arguments would be displayed nicely (including complex objects)
I've tried unpacking arguments and recursively call this[methodName] on each of them (which would be log.debug() or log.error() etc..), and then concatenate the results, but I can't get it right. How to achieve this?
Thanks!