1.1.0 • Published 4 years ago

android-style-logging v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Android Style Logging

Colorful logging output for node console applications using basic log levels. I created this package because I really liked the way Android implemented logging level output in the console when using Android Studio.

log levelfunction
verbosev
debugd
infoi
warnw
errore

Installation

Install using npm

npm i android-style-logging

Usage

const Log = require('android-style-logging')
const log = new Log();
const TAG = 'ASL'

log.v(TAG, "This should show up as a verbose output")
log.d(TAG, "This should show up as a debug output")
log.i(TAG, "This should show up as an info output")
log.w(TAG, "This should show up as a warn output")
log.e(TAG, "This should show up as an error output")

Methods

functionparam(s)Description
v(TAG \, MSG)verbose output
d(TAG \, MSG)debug output
i(TAG \, MSG)info output
w(TAG \, MSG)warn output
e(TAG \, MSG)error output
showfgColorsdisplays all possible foreground colors
showbgColorsdisplays all possible background colors
setColor(colorObject)change color options
setEnabled(enabledObject)change which log levels are enabled/disabled

Color Formats

I am using the ANSI escape character with 8 bit colors. You can read more about it on this stackoverflow question.

You can put your own colors in the config:

Foreground color example: [38;5;232m

Background color example: [48;5;232m

You can use showfgColors and showbgColors to easily choose the colors you want.

Environment Variables

variablevaluedescription
ASL_LOGoptions to enableenables logging levels

The following example will enable info, verbose and error regardless of your config. Options will never be disabled with this environment variable

ASL_LOG=ive node app.js

Default Config

The cosmiconfig package is used for runtime configuration. Create an RC file in your project root directory to overwrite any settings that you wish to modify.

You can also pass in a configuration object when instantiating a Log object:

const configObject = {
  "showExample": true
}
const Log = require('android-style-logging')
const log = new Log(configObject);

The default configuration is:

.androidstyleloggingrc.js

module.exports = {
  "colors":{
    "v": {
      "tag": {
        "fgColor": "black",
        "bgColor": "white"
      },
      "message": {
        "fgColor": "white",
        "bgColor": ""
      }
    },
    "d": {
      "tag": {
        "fgColor": "black",
        "bgColor": "green"
      },
      "message": {
        "fgColor": "green",
        "bgColor": ""
      }
    },
    "i": {
      "tag": {
        "fgColor": "black",
        "bgColor": "cyan"
      },
      "message": {
        "fgColor": "cyan",
        "bgColor": ""
      }
    },
    "w": {
      "tag": {
        "fgColor": "black",
        "bgColor": "yellow"
      },
      "message": {
        "fgColor": "yellow",
        "bgColor": ""
      }
    },
    "e": {
      "tag": {
        "fgColor": "white",
        "bgColor": "red"
      },
      "message": {
        "fgColor": "red",
        "bgColor": ""
      }
    }
  },
  "enabled": {
    "v": true,
    "d": true,
    "i": true,
    "w": true,
    "e": true
  },
  "showSettings": false,
  "showExample": false
}