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:
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")();
useAppVersion.ts
App.vue
Chrome Console
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 .
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.
Of course, console. log() will reduce your program's performance since it takes computational time.
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.
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:
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…
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With