Skip to content

rvrheenen/consolert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

consolert

Build Status Last commit Downloads License: MIT Donate Donate Donate

NPM

Created by: Rick van Rheenen

Improved NodeJS console Extends console logging by adding prefix, timestamp and colors. This package will enhance the output of your project simply by overwriting the console object, no other code adjustments are needed.

Installation

npm install consolert

Usage

To overwrite the built-in console:

"Normal" way to do it:

const Consolert = require("consolert")
const console = new Consolert(options)

Alternative oneliner:

const console = new (require("consolert"))(options)
options

optional object containing any or none of the following k/v pairs:

tag      - String   (default: "N/A")
debug    - Boolean  (default: false) 
showTime - Boolean  (default: true) 
showTag  - Boolean  (default: true) 
showType - Boolean  (default: true)

methods

setConfig [options]

Options can be changed after initializing by passing the required options as an object to the setConfig method:

const Consolert = require("consolert")
const console = new Consolert()
console.setConfig({tag: "TAG", debug: true})

Working example:

index.js

const Consolert = require("consolert")
const console = new Consolert({tag:"APP", debug:true})

console.log("Tagged by Consolert")
console.debug("eee bugses!")
console.warn("Something might be broken..")
console.error("Something is definitely broken!")

terminal:

foo@bar:~$ node logs/index.js 
LOG   2020-04-17 15:52:53 [APP] Tagged by Consolert
DEBUG 2020-04-17 15:52:53 [APP] eee bugses!
WARN  2020-04-17 15:52:53 [APP] Something might be broken..
ERROR 2020-04-17 15:52:53 [APP] Something is definitely broken!

(note these would be colored according to their type of method, but markdown doesn't let me show that..)

todo

  • publish
  • add tests
  • allow config adjustment after initialization
  • expand debug to be levels instead of boolean
  • allow customizing onlyInDebugMode methods
  • allow for custom stdout and stderr
  • allow for custom colors
  • allow custom timestamp