Logging trace is one of the basic needs of any
programming language. It does not matter how good your code is unless it is not
debugable. For debugging, Logging is one of the best tools. It is
easy to follow. If you are working on
local system This could be your best mate while
LOG WELL, LIVE WELL
Here in this tutorial, I will explain. How you can build a super cool logger with just minimal code
(a few lines of the code). That is also without using any third-party tool.
console.log partially implements functions like
java. But it doesn't support all the features.
So how we should log formatted messages?
Using String template.
Cool! But we can see for each parameter in the template, we have to give you a constant/variable. It will be hard to create variables just to log something.
Using Vargs in
console.log However, we can't arrange the message with variables. This is very useful to print anything just by separating value from comma. But this can not be used for logging something useful.
- Unpredicted implementation
Let's see by an example:
If you try to append the object with
string it converts to the string which is
[object Object] for object and appended.
Use Partial printf like feature in
Here you can see,
console.log does support
printf like functions. However, this is limited until
%d [number]. Mozilla does support Precision formatting something like
Deno is highly inspired by Go. Just like Go, Deno also has
fmt module in the standard library. This module/package is a combination of multiple sub-modules like
fmt module is still in work-in-progress status according to developers.
Definition of printf:
printf takes string and
vars of unknown. The format is to define what the format of the string will look like. args could be anything according to the format you wanted.
Sample of printf:
The following verbs are supported:
|print a literal percent|
|evaluate arg as boolean, print |
|eval as number, print binary|
|eval as number, print character corr. to the codePoint|
|eval as number, print octal|
|print as hex (ff FF), treat string as list of bytes|
|print number in scientific/exponent format 1.123123e+01|
|print number as float with decimal point and no exponent|
|use %e %E or %f %F depending on size of argument|
|type of arg, as returned by |
|value of argument in 'default' format (see below)|
|argument as formatted by |
Verbs with Width and Precision
Sample of Width and Precision:
You can write more complex examples like padding and get the value of the
vargs passed to
Sample of Padding:
The conversion of
hex color is so simple.
More Complex use index of
THE DOOR IS RED HERE THE WRITER USES THE COLOR TO EXPRESS HIS ANGER
Another issue with
console.log, It does not support colors out of the box. We end-up adding libraries like colors in NodeJs. The browser has some additional support for
css console.log. But that will not work in NodeJs
Thanks to Deno's Colors module. Now we can easily color our life.
colors module also supports rgb colors
From Deno source:
Create a constructor and define default values for log level and format:
Define getter setter for level and format:
This is to hide the definition. You can remove setters if you don't want user to modify the values.
Add basic methods:
If you notice, I am using
sprintf before using
printf. Since I want the full control on the printing message like adding colors and using the dynamic format. I have to use
sprintf instead of
Let's allow the user to modify format at runtime:
Nice! Well done! We have done it!!
The entire implementation of
Logger class can be found in
How to use sample can be found in
For more examples like this, visit: https://decipher.dev/deno-by-example/
I hope you like this tutorial. let me know your feedback in the comment. Please support(🙏🙏) by subscribing and clapping on https://deepak-v.medium.com/.