0

I'm trying to create an overload for console.log() but my function does not process strings like console.log can,

for example, this is working:

console.log("variableName = %s", variableName);

However, it breaks my overloaded function which calls console.log() with a message as the string:

function consoleLog(message) {
    consoleMessageLogger(message, LOG_TYPE_LOG);
}

I'm aware that I could use a new syntax like this:

consoleLog(`variableName: ${variableName}`);

However I'm trying to make it work the old way as well - like this:

consoleLog("variableName = %s", variableName); // my overloaded function

I assume the solution involves ... and arguments (IArguments), but I'm not sure how to use it exactly, also it would be nice if I can find some code that already handles all available types - like "%s", "%d", etc.

Yovav
  • 2,557
  • 2
  • 32
  • 53
  • 3
    What is `consoleMessageLogger`? – loganfsmyth Apr 09 '20 at 03:46
  • 2
    Your `consoleLog()` is expecting one argument, and you're passing two ‍♂️ – Code Maniac Apr 09 '20 at 04:04
  • @loganfsmyth - my function consoleMessageLogger() is taking another parameter to determine the logging level (log / info / warn / error), however, the message is defined as a string and it is being passed from consoleLog() – Yovav Apr 10 '20 at 18:34
  • @CodeManiac - the whole point of this question is how to combine these parameters just like you can do it when using console.log("variableName = %s", variableName); – Yovav Apr 10 '20 at 18:35
  • MDN has many examples for how to use [the `arguments` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) or [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) (notice you'd use either, not both, probably a rest parameter is easier), and the duplicate has many examples on how to deal with *format strings*. – Bergi Apr 10 '20 at 18:44
  • @Bergi - you closed this question but the String.Format is not a good answer since I'm trying to use the string exactly as shown in my example, without having to format it first – Yovav Apr 12 '20 at 02:12
  • No, you shouldn't format it *first*, you should format it *inside* your `consoleLog` implementation. The duplicate is meant to give useful advice on how to write that formatting code. – Bergi Apr 12 '20 at 11:06
  • @Bergi - it is a different solution, I don't want to have to change any of my existing string using a format method, I want all existing code to be used the same way it can be used for the function console.log("%s", variable) - so the answer you found is not solving the problem. – Yovav Apr 13 '20 at 19:53
  • Again, I never said you should change any of the calls to `consoleLog`. You should use [this code](https://stackoverflow.com/a/4795914/1048572) (or [this](https://stackoverflow.com/a/31007976/1048572), or [that](https://stackoverflow.com/a/23087471/1048572), or one of the other approaches in the dupe) to implement your `consoleLog` function - that's exactly what you had been asking for. Sure, your function wouldn't `return` the formatted string but pass it to your custom logging solution, but otherwise that's exactly the solution. – Bergi Apr 13 '20 at 20:02
  • @Bergi - thanks. that works now. – Yovav Apr 14 '20 at 18:01

0 Answers0