@uandev/log
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

UANDEV - Log Module

My custom logging solution for all of my apps.

Usage

This module works best with Typescript. It provides three items:

Level

An enum detailing the various available log levels

LogWriter

An interface describing the available functions for writing events. It contains the following functions: trace, debug, info, warn, error, and fatal. They all have the same signature: (message: string, data: any)

GetWriter

A function with the signature: (namespace: string) => LogWriter that returns a LogWriter object for writing events for the provided namespace.

Setup

A function with the signature: (settings: ISettings) => void. It allows you to make changes to the Log module during runtime.

ISettings

An interface for modifying the settings of the Log module. It contains the following options:

appName

The name of the application as it should appear in the log. It will also be used ast the base of the namespace for events. Default: 'defaultApp'

level

The level minimum level of log output. Use the enum Level to set this. Default: info

color

Output colored log events. Very helpful for viewing during development. Not recommended for production when events are sent to a file. Default: false

timestampFormat

The format you would like to use for event timestamps. This is just a pass through directly to the moment time formatter. Default: 'YYYY-MM-DD HH:mm:ss'

enabled

Enable or disable log events. Default: true

output

A function to use for output of events. This function should have the signature: (output: string) => void. If you want to completely disable output, you can also pass null. This is mostly useful for the unit tests. Default: console.error

cluster

A boolean that signals whether or not to add cluster information to the namespace. When enabled events generated by master will be like this: myapp:master:some:name:space. Worker processes will generate events like this: myapp:worker:1:some:name:space.

Example

import { Log } from '@uandev/log'
Log.Setup({
  color: true,
  output: console.log,
  level: Log.Level.warn,
  appName: 'myapp'
})

let w = Log.GetWriter('test')

w.warn('This will output', { sample: 1 })
w.info('This will not be output')

The output will look like this:

2019-07-04 12:48:32 thehost myapp[9828]: warn myapp:test [sample='1'] This will output

Readme

Keywords

none

Package Sidebar

Install

npm i @uandev/log

Weekly Downloads

14

Version

3.0.0

License

MIT

Unpacked Size

25.6 kB

Total Files

11

Last publish

Collaborators

  • driverjb