3.1.0 • Published 4 years ago

mega-nice-log v3.1.0

Weekly downloads
90
License
-
Repository
github
Last release
4 years ago

Mega Nice Log

A mega nice log library. Works in Node and in browsers.

Install

npm install mega-nice-log

Overview

Instantiate a logger

Instantiate a new logger per file. The constructor takes its filename as the parameter. When you are in Node simply use __filename.

import Log from 'mega-nice-log'

let log = new Log(__filename)

When you are in a browser depict the filename.

import Log from 'mega-nice-log'

let log = new Log('HttpServer.ts')

Log

Now you can log. Here are the five log levels that you can use.

  • log.error: For errors
  • log.warning: For warnings
  • log.info: For information that you always want to see in your log. May help the admin.
  • log.debug: For very fine grained information about the execution of the code.
  • log.insane: For very detailed and lengthy output that you only want to see if it is really relevant. When you are debugging exactly that file.

Like in any other logging library error will only print errors while warning will print errors and warnings and so on.

Prepended log information

The logger will prepend the filename without its extension assuming it is equal to a class name.

HttpServer$ Starting service...
HttpServer$ Running on port 80

If your file contains several classes you can depict a class name.

let l = log.cls('Request')
l.debug('this.url =', this.url)
HttpServer.ts > Request$ this.url = 'http://mega-nice.com/json'

Additionally you can and you always should depict the name of the function the logger is logging in.

let l = log.fn('handler')
l.debug('Handling incoming request...')
l.debug('request =', request)
HttpServer.ts > HttpServer$ Entering HttpServer.handler
HttpServer.ts > HttpServer.handler$ Handling incoming request...
HttpServer.ts > HttpServer.handler$ request = Request { url: 'http://mega-nice.com/json' }

When instantiating the function specific logger it will print a Entering ... message which is very useful when trying to follow the flow of the program.

Different color per log level

The prepended information (filename, class name, function name) will be printed in different colors corresponding to the used log level.

  • error: red
  • warning: yellow
  • info: white
  • debug: cyan
  • insane: cyan

Set the log level

You can set the log level directly in the constructor of the logger.

let log = new Log(__filename, 'insane')

Or you can set it on a static property on the Log class.

Log.levels = {
  'HttpServer.ts': 'info',
  'HttpServer': 'debug',
  'HttpServer.handler': 'insane'
}

You can either depict the filename, the class name or the function name in conjunction with its class name.

Set a global log level

You can set a global log level which will be used if there is no other declaration.

Log.globalLevel = 'warning'

When you run your tests it may be useful to silence all the output.

Log.gobalLevel = 'silent'

Use colors in the first message parameter

The first given message parameter may contain color definitions.

log.debug('The value was color(magenta)undefined')

This will output the word undefined with a magenta color.

The following colors are supported. Those are the standard console colors: reset, bright, dim, underscore, blink, reverse, hidden, black, red, green, yellow, blue, magenta, cyan, white, bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.1.0

4 years ago

1.2.1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago