that-logger
v1.0.7
Published
You might be thinking something like this while reading this:
Downloads
2
Readme
that-logger
You might be thinking something like this while reading this:
that-logger
? Yet another logger with basic coloring functionality? Why do we need this?
On the surface? Perhaps there is some truth to that. But that-logger
is an especially powerful and useful logger for one reason that we'll get to later.
Creating and using a logger.
First, we need to create our logger to log things... obviously.
import Logger from 'that-logger';
const log = new Logger();
Now... let's log something.
log.info('Some basic info.');
Internally, this gets converted to:
log.log('info', 'Some basic info.');
Which outputs something that looks like this:
INFO Some basic info.
The reason for this is that that-logger
operates based on an extendable prefix system. For example, you could import prefixes
from that-logger
import Logger, { prefixes } from 'that-logger';
import chalk from 'chalk'; //For coloring
Then, you can make a new logger with a new prefix.
const log = new Logger({
prefixes: {
...prefixes,
success: chalk.green('SUCCESS');
}
});
Now, once we do log.log('success', msg)
, we'll have it prefixed with SUCCESS
.
Here are the main logging methods:
Logger.info
Logger.warn
Logger.debug
Logger.error
Ignoring Debug Logs
Doing other things like ignoring specific log prefixes is as easy as pie too.
const log = new Logger({
ignore: [ 'debug' ]
});
Sub-Loggers
There's a reason that-logger
stands out, and that's sub-loggers. Let's make one ourselves:
const sub = log.create('Sub');
Now, sub
is a new log with the same methods, like info
, warn
, even log
. It's an instance of Logger
, just like the main logger. However, when we try logging with it, with for example something like:
sub.info('Cool, cool cool cool, cool.');
We get the following output:
Sub
INFO Cool, cool cool cool, cool.
And of course, sub
also supports .create
. With sub-loggers, you can easily organize your logs into neat packages which makes life easier.
Even better, you can handle errors much easier since it's super easy to see where the error came from.
If you want to learn more, read the source code, it's not too complex (other then the match
function), and have a good time using that-logger
!