-
-
Notifications
You must be signed in to change notification settings - Fork 7
Getting Started
The default logger is the simplest way to use Loggin'JS.
const loggin = require('loggin-js');
loggin.debug('Check this log out!!', { foo: 'var' }, { channel: 'Wabalabadubdub!' });
You can create a logger that's based on custom arguments by using the .logger
function.
By default if no arguments are passed, it will return a logger with a console notifier attached, and a level of DEBUG (see here for more info), this means it will output all logs to the console.
const loggin = require('loggin-js');
loggin.logger();
You can also pass in a string representing the name of a premade logger, for example the following code will return a logger with a file notifier attached instead:
loggin.logger('file');
loggin.logger('http', { /* opts */ });
Alternatively, you can pass in an object with a set of options to generate a logger, this example will return a logger with two notifiers (file & console) and set to level error
strict, this means it will only output the exact level of error
:
loggin.logger({
level: severity('error', { strict: true }),
notifiers: [fileNotifier, consoleNotifier]
});
Notice: you can add, remove and change the notifiers after creating them.
See here for info on creating notifiers.
See here for docs for options and premades.
Now let's see how you could configure your logger a bit.
Mostly every aspect of loggin-js is configurable or editable, like the format, what logs are output, if they are colored, etc...
There is also a set of premade instances of all common utilities, this is true for [logger
, notifier
, severity
, formatter
],
For example, this code will return a minimal formatter:
loggin.formatter('minimal');
Loggin-js uses strif for template procesing. Check out the strif repo for more details on how to create your own templates.
You can set a premade formatter or a strif.Template
(see here for more info) on a logger:
const formatter = loggin.formatter('minimal');
logger.formatter(formatter);
logger.debug('A cool message');
Should output:
$ example.js - A cool message
You can set a json formatter, the log will output a json string
const formatter = loggin.formatter('json');
You can also specify one or more notifiers, which could log to a file, to the console, to a remote service or some other custom notifier.
The easiest way of creating a notifier is by using the .notifier
function:
const consoleNotif = loggin.notifier('console', { level: 'debug' });
consoleNotif.color(true);
Alternatively you can also use the available class .Notifier.File
to create a logger:
const fileNotif = new loggin.Notifier.File({ level: 'DEBUG' });
With file notifiers you can specify where to send the logs based on some Severity using the .pipe
method:
fileNotif.pipe(Severity.ERROR, 'logs/error-logs.log');
fileNotif.pipe(Severity.DEBUG, 'logs/debug-logs.log');
You can add them to the logger like this:
// Adds notifier
logger.notifier(consoleNotif, fileNotif);
// Overwrites all loggers
logger.setNotifers([consoleNotif, fileNotif]);
Above logger will send every log through both notifiers:
- consoleNotif will log everything to the console.
-
fileNotif will log ERROR logs to file
logs/error-logs.log
and everything tologs/debug-logs.log
.
You can get access to notifiers after creating a logger, whether you created a default logger or you just want to access the loggers.
You can do it by using the Logger.getNotifier(name)
method. Here is an example:
let logger = loggin.logger('console');
let csol = logger.getNotifier('console');
csol.color(true);
After creating the logger we can change most of the options, like the level, the channel, etc... For example:
const logger = loggin.logger();
logger
.level('DEBUG')
.color(true)
.channel('super-app');
logger.debug('A cool message');
Remember that all Logger configuration methods propagate to all the Notifiers that they have. If you just want to affect one notifier, you must have created that notifier yourself, and passed it into the logger.
We can set a level in three ways:
- Passing a string (info):
logger.level('DEBUG');
- Passing an int (info):
logger.level(9);
- Using
loggin.severity
function:logging.level(loggin.severity('INFO'));
You can create you own Notifiers, Formatters, Loggers, etc... see here for more examples!
If you want more control over which logs are output, you can pass in a ignore
function to the logger options.
If passed, the ignore
function will be called before each log is propagated to the Notifiers.
Ignore
will be called with the log
as the first argument, and the selected notifier
as the second argument. If more than one notifier is set, the function will be called for each notifier
let logger = loggin.logger({
ignore(log, notifier) {
return log.level.name == 'INFO';
},
preNotify(log, notifier) {
log.message = '<%b------> <%y' + log.message.toLowerCase() + '> <%b------>';
log.level.name = log.level.name.toLowerCase();
},
})
Additionally, there is a
preNotify
callback function that will be called before a log is propagated.
You can configure almost every aspect of the logger. You can customize the format of your logs, the output channel (Notifiers), what logs are output (Severity), etc...
Check the /examples
folder for more examples.
The easiest way to create a logger is by using the .logger
method.
.logger
can return several pre-configured types of loggers, you can construct you own. But, let's make it simple for now by
creating the simplest logger posible:
// Call `.logger` and boom, you are rollin' ;)
const logger = loggin.logger();
// Now you can start loggin'
logger.info('A good message');
logger.error('Not so good, eh?', null, { channel: 'other-channel' });
By default .logger()
will return a logger set to level DEBUG with a detailed formatter,
which would output something similiar to this through the console:
$ 2018-06-02 root example.js - INFO - A good message
$ 2018-06-02 root other-channel - ERROR - Not so good, eh?
It will show colored output by default!
Here is an advanced example:
const fileNotif = loggin.notifier('file');
const consoleNotif = loggin.notifier('console');
fileNotif.pipe('debug', './debug.log');
fileNotif.pipe('error', './error.log');
consoleNotif.color(true);
const logger = loggin.logger({
notifier: [fileNotif, consoleNotif],
channel: 'my-app',
preNotify(log) {
log.message = log.message + ' - ' + hash(log);
}
ignore(log, notifier) {
return notifier.name === 'console' && DEBUG === false;
},
});
// Now you can start loggin'
logger.info('A good message');
logger.error('Not so good, eh?', null, { channel: 'other-channel' });
NOTE:
preNotify
is called beforeignore
, and before propagating the log to Notifiers.
NOTE:preNotify
allows modification of the log.
If you find an outdated document or you think that it does miss to explain some point or there is some issue, please leave an issue! Or make a PR with the fix!!