A logging package that lets you color code your logging namespaces. Easily trace just your database or error logic, by following the corresponding colors. Additionally, each logger is individually configurable, and the output aligns for easy reading, even if color-coding is not an option. The goal is to make logs as easy to read as possible.
Notice that this package is still not officially released, and is under development. All releases should be stable, but are currently only tested on a Mac, use at your own risk, currently
npm i easy-log
Using const logger = require('easy-log')('app')
creates a logging function logger
with the namespace "app", which means the specified logs will only output when that name-space is enabled. Enabling/disabling namespaces can be done dynamically at run time in the code, statically at the beginning in the namescript, or a mix of both. Each logger can be customized individually of each other. You can set, change, and toggle colors, formatting, stack tracing, and even the entire logger itself. Read on for specific syntax and examples of how to do so.
A basic use case with a few different name-spaced loggers, and a couple different files
Example app.js
const express = require('express')
, basicLogger = require('easy-log')('app:basic', { colorCode: 199 })
, dbLogger = require('easy-log')('app:db:', { colorCode: 226 })
, mongoose = require('mongoose');
const app = express();
const name = "Example Application";
basicLogger(`Booting ${name}`);
mongoose.connect("mongodb://localhost/example", { useNewUrlParser: true })
.then(() => { dbLogger(`Mongod DB connected successfully`); })
.catch((err) => { dbLogger(`Mongo DB could not connect: ${err}`); });
const port = process.env.PORT || 3000;
app.listen(port, () => { basicLogger(`App listening on port ${port}`); });
// Use some imaginary worker file
require('./worker');
Example worker.js
const dbLogger = require('easy-log')('app:db', { colorCode: 40 })
, basicLogger = require('easy-log')('app:basic', { colorCode: 45 });
function dbWork() {
dbLogger('doing lots of uninteresting database work');
setTimeout(dbWork, Math.random() * 1000);
}
dbWork();
function basicWork() {
basicLogger('doing some basic work:b');
setTimeout(basicWork, Math.random() * 2000);
}
basicWork();
When you create a logger, simply pass it a namespace as well, and it will only output when that namespace is enabled either in code or in the run script.
const logger = require('easy-log')(); // Will default to '' and will always work
const logger2 = require('easy-log')('debugging') // Now will only output when the 'debugging' namespace is enabled
Note that a logger without a namespace will always output, and in fact, cannot be turned off.
When you want to enable or disable a namespace you currently have two options.
logger.enable();
logger.disable();
Enabling a namespace
DEBUG=debugging
// OR
DEBUG=debugging,other-namespace
// OR
DEBUG=debugging other-namespace
Notice the runs script can set the DEBUG environmental variable(s) with commas, or spaces.
Specifically Disabling a namespace
DEBUG=debugging,-other-namespace
The -
tells the logger to disable. If you put DEBUG=debugging,-debugging
, the last one gets run last and so the debugging
namespace logger will not work. This works really well in conjunction with wildcards.
Instead of enabling every namespace, or listing every one in the run script, you can use wildcards. Wildcards can only be added in the run scripts
DEBUG=* // Will run every namespace (including some global node ones you might not want)
DEBUG=app:* // Will run every namespace that is a child of `app` (`app:db`, `app:error`, etc)
DEBUG=app:*,-app:db // Will run every namespace that is a child of `app` EXCEPT `app:db`
You may have noticed that each logger almost looks like a lite error stack trace, including the location in the code of the logger. This is intentional and the default behaviour, although it can be overridden. This is to give the developer more information about the program and where each output log is coming from, allowing them to trace through the program with ease.
You also may have noticed that every logger lines up, right aligned. This makes it easier to ready, and currently is the default and only behaviour, but if it is not suitable for your use case, please submit a feature request on github and I will make a way to configure it.
When a logger is created, you can also pass it a set of options that will change how the logger acts. Right now these can only be configured when the logger is created. Below is a list of current options and what they do.
const logger = require('easy-log')('app', { colorCode: 201, includeLineNumber: false });
colorCode | Integer (supported ones) | Specify a color for the logger | { colorCode: 201 } | Calculated from namespace string |
includeFunction | Boolean | Whether to list the function where the logger was called | { includeFunction: false } | true |
includeFile | Boolean | Whether to list the file where the logger was called | { includeFunction: false } | `true` |
includeLineNumber | Boolean | Whether to list the line where the logger was called | { includeFunction: false } | true |
**That's it! So far this is what I have. Beware, this is not yet tested on windows, linux, or browsers, and I may need to tweak things to make those all work. **
If you have a feature suggestion PLEASE let me know so I can incorporate it as quickly as possible. You can do so on my github, by following the Contributing Guidelines I have and using the issue
template I created: Ask for a new feature
Below is a table of supported color codes for you to choose from when configuring colors