Skip to content

Repository files navigation

Node.js versionNPM versionsemantic-releaseGreenkeeper badgebuild with travis-citested with jestCoverage Statusnpm versionBCH complianceMaintainability

nx-logger

nx-logger is a logging utility that grows with the application

  • TypeScript2.9
  • Jest unit testing and code coverage
  • Type definitions for Node.js v6.x (LTS) and Jest

Installation

npm i @raynode/nx-logger

Migrations

As nxLogger namespace was removed, the access to the internals like interfaces or the log levels was changed. This will result in the following being invalid:

import{nxLogger,create}from'@raynode/nx-logger'constlog=create({verbosity: nxLogger.DEBUG})

it will be like this now:

import{LogLevel,create}from'@raynode/nx-logger'constlog=create({verbosity: LogLevel.DEBUG})

Further the nxLogger is not necessary any more to access the interfaces

// beforeimport{nxLogger}from'@raynode/nx-logger'typemyLog=nxLogger.Log// afterimport{Log}from'@raynode/nx-logger'typemyLog=Log// orimport{LogasmyLog}from'@raynode/nx-logger'

Usage

Basic usage would just render some console output

import{create}from'@raynode/nx-logger'constlog=create('my-project')log('start-up')

Will run this line of console output

console.log('my-project - start-up')

As I love Debug from visionmedia, there is already a transport to transform your logging.

import{create,configure}from'@raynode/nx-logger'import{transport}from'@raynode/nx-logger-debug'configure({ transport })constlog=create('my-project')log('start-up')

This will now send the log message through debug, including the namespaces

Features

Namespaces

Each logging instance can have its own namespace. They are designed to be inherited and used per file or per context.

You could configure it per file (with default transport)

// app.tsconfigure({namespace: ['project']})constcontext=create('app')// Result: project - app : msg// utils.tsconstcontext=create('utils')// Result: project - utils : msg

Or you can use it as per request

configure({namespace: ['project']})constappLog=create('express')constapp=express()app.use((req,res,next)=>{req.log=appLog(req.url)next()})app.get('/something/*',(req,res)=>{req.log('making')// Result: project - express - /something/more : making})

Transports

The nx-logger is written in a way that lets you start a project and have simple logging output. And after the project grows you'll only have to attach another transport to send the log output directly to your logging servers or services (like: Loggly).

Verbosity

Like in console there are some functions to define the verbosity of the logging call.

constcontext=create({verbosity: 5,// default})context.debug('DEBUG-level: 10')context.info('INFO-level: 7')context.log('LOG-level: 5')context('LOG-level: 5')context.warn('WARN-level: 3')context.error('ERROR-level: 1')

The verbosity is set per namespace to more easily debug problems during development.

Functionhandlers

Sometime we need to know when a specific function is called, and we write code like this:

constmyFunction=(a,b)=>{context.log('myFunction',[a,b])// ...}myFunction(1,2)// myFunction [1, 2]

nx-logger has a logging handler for this case:

constmyFunction=context.on((a,b)=>{// ...},'myFunction')myFunction(1,2)// myFunction [1, 2]

The context.on(callback, [namespace], [configuration]) feature will enable you to get information whenever the defined function is called. Combined with the conditions feature it might look like this

constsignup=context.on((user: User)=>db.run('create',user),'signup-user',{transport: join(transport,createTransport(logglyConfiguration)),})

Conditions

When a project grows and it is necessary to enable new logging functions, it might be nice to have a possibility to join transports together.

import{create,join}from'@raynode/nx-logger'import{transport}from'@raynode/nx-logger-debug'import{createTransport}from'@raynode/nx-logger-loggly'constcontext=create({transport: join(transport,createTransport(logglyConfiguration))})context.log('message')// will show up in the console through debug and be transmitted to loggly via loggly-transport.

It might also be good to decide which messages are used in some transport

import{create,split,LogLevel}from'@raynode/nx-logger'import{transport}from'@raynode/nx-logger-debug'import{createTransport}from'@raynode/nx-logger-loggly'constdecide=(configuration,messages,verbosity)=>{if(verbosity>LogLevel.LOG)returntrue// use debugelsereturnfalse// use loggly for LOG and below}constcontext=create({transport: split(decide,transport,createTransport(logglyConfiguration))})context.log('important')// will be transmitted to loggly via loggly-transport.context.info('just infos')// will show up in the console through debug

Contributing

If you want to help with this project, just leave a bugreport or pull request. I'll try to come back to you as soon as possible

License

MIT

About

node meta-logging library

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages