Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend console.log which can accept args

I've been working on extending console.log to make a colorful one that keeps the stack trace as to where it has been called. I've tried some solutions, but finally reach this point:

export const colorizedLog = (
  text: string,
  status: keyof typeof ColorStatus = "dark",
  ...args: any
) =>
  console.log.bind(
    console,
    `%c ${text}`,
    `font-weight:bold; color: ${ColorStatus[status]}`,
    ...args
  );

With this little binding, we will be able to keep the stack trace, but the problem here is we have to use it with an annoying extra () at the end, because of returning value is the function itself which is the result of bind:

 colorizedLog(
    "Title:",
    "info",
    "This is a working example")();

The top other resources that I've read through are as below:

  1. Extending console.log
  2. Macro using babel- kent.c dodds
  3. Webpack Define plugin

const colorizedLog = (text, color= "#40a7e3", ...args) =>
  console.log.bind(
    console,
    `%c ${text}`,
    `font-weight:bold; color:${color}`,
    ...args
  );
colorizedLog("Title:", "#40a7e3", "This is a working example")();

Update: Stack trace

useAppVersion.ts enter image description here

App.vue enter image description here

Chrome Console enter image description here

like image 986
SeyyedKhandon Avatar asked Feb 13 '21 06:02

SeyyedKhandon


People also ask

How do you wrap a console log?

Type it and press Ctrl+Alt+W + W . Another way to console. log your variables is to simply place your mouse cursor on them and then wrap them on the line below with Ctrl+Alt+W + Down or the line above with Ctrl+Alt+W + Up .

Can I override console log?

log. To override a console method, we just need to redefine how the method is executed. You'll need to wrap your code to prevent the access of other functions to the private (original) method. Note: The previous code would do the final trick.

Does console log reduce performance?

Of course, console. log() will reduce your program's performance since it takes computational time.

How do you store console output in variables?

If you want to do this to an object that has been already logged (one time thing), chrome console offers a good solution. Hover over the printed object in the console, right click, then click on "Store as Global Variable". Chrome will assign it to a temporary var name for you which you can use in the console.

How to console Log objects in JavaScript?

The basic method is used console.log (object) to console log objects in JavaScript. The Console method log () outputs a message to the web console. Note: you must only log the object. For example, this won’t work:

What is VAR1 and var2 in console logs?

var var1 = 'foo', var2 = 'bar' console.log ('%s, %s', var1, var2) Which browser are you using? It’s certainly odd that those variable names are getting interpreted as strings…

How do I log a variable as an argument type?

Argument types can be different, that’s quite handy when you need to specify what you’re logging: This way you’ll know the variable name you’re logging. You can also log a variable as an object property by wrapping it into curly braces: This approach is useful when you need to log multiple variables or items to the console.

How do I get the command line arguments of a program?

In a console app there are two ways to get commands: Command line arguments passed into your program via Main (string [] args). User input from Console.ReadLine () (which you then split into a string []). After getting a command, you have to parse it to figure out what code to execute.


Video Answer


1 Answers

You can make it immediately invoked:

const colorizedLog = (text, color= "#40a7e3", ...args) =>
  console.log.bind(
    console,
    `%c ${text}`,
    `font-weight:bold; color:${color}`,
    ...args
  )();

colorizedLog("Title:", "#40a7e3", "This is a working example");
like image 95
Julius Dzidzevičius Avatar answered Nov 04 '22 00:11

Julius Dzidzevičius