Simple Log
I appreciate all of the robust logging modules available on npm, but sometimes I just want something simple to use that collects a decent amount of useful information. I have tailored this module to fit my needs and published it to npm in case someone else might find it useful. Also, to make it easier for me to install into my other projects.
The latest version (2.0.0) uses the debug
npm module at its core. This version is a slick wrapper
around that module providing the things that I value most.
Configuration
Each configuration option for Simple Log can be found in the settings
key of the package. Each
individual setting has its own get/set methods.
Level
The log level at which events will be written. Levels are: trace
, debug
, info
, warn
, error
and fatal
.
Cluster Mode
Instructs the module to write in cluster mode. This means that if you are running app using NodeJS
clustering, the Simple Log module will inject cluster information into your namespace. For example:
Say you initialized a writer with the global namespace myapp
and the namespace test
. Then consider:
Cluster Mode Disabled
Output: myapp:test Some message +1ms
Cluster Mode Enabled
Output(Master): myapp:master:test Some message +1ms
Output(Worker): myapp:worker:1:test Some message +1ms
Global Namespace
This is the base namespace you will set for your application. This allows you to filter for just your app when outputting log entries. See debug for details.
Configuration Example
const log = require('@uandev/simple-log')
log.settings.level.set('trace')
log.settings.globalNamespace.set('myapp')
log.settings.cluster.disable() //use disable and enable here. cluster.get() returns 'on' or 'off'
Usage
In order to generate a writer you must call the init function in the simple-log module. It takes a single parameter - namespace. For an example, lets say you need to log an api endpoint that handles user data. You would maybe create a logger like this:
const log = require('@uandev/simple-log').init('api:user')
app.get('/', (req, res) => {
log.info('User loaded', { user: req.session.user })
})
Let's assume that you had already called configured the module during an init script for your server and it was set to log info level and up, clustering disabled, and the global namespace is myapp. The output might look something like:
myapp:api:user YYYY.MM.DD HH:mm:ss MyServer MYAPP[1234]: INFO [user='someUser'] User Loaded +3ms
The same example with clustering enabled assuming your routes are workers would look like:
myapp:worker:1:api:user YYYY.MM.DD HH:mm:ss MyServer MYAPP[1234]: INFO [user='someUser'] User Loaded +3ms