npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@raynode/nx-logger

v6.1.2

Published

Logging utility that grows with the application

Downloads

75

Readme

Node.js version NPM version semantic-release Greenkeeper badge build with travis-ci tested with jest Coverage Status npm version BCH compliance Maintainability

nx-logger

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

  • TypeScript 2.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'

const log = create({ verbosity: nxLogger.DEBUG })

it will be like this now:

import { LogLevel, create } from '@raynode/nx-logger'

const log = create({ verbosity: LogLevel.DEBUG })

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

// before
import { nxLogger } from '@raynode/nx-logger'
type myLog = nxLogger.Log

// after
import { Log } from '@raynode/nx-logger'
type myLog = Log
// or
import { Log as myLog } from '@raynode/nx-logger'

Usage

Basic usage would just render some console output

import { create } from '@raynode/nx-logger'

const log = 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 })

const log = 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.ts
configure({ namespace: ['project'] })
const context = create('app')
// Result: project - app : msg

// utils.ts
const context = create('utils')
// Result: project - utils : msg

Or you can use it as per request

configure({ namespace: ['project'] })
const appLog = create('express')
const app = 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.

const context = 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:


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

myFunction(1, 2) // myFunction [1, 2]

nx-logger has a logging handler for this case:


const myFunction = 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

const signup = 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'

const context = 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'

const decide = (configuration, messages, verbosity) => {
  if(verbosity > LogLevel.LOG)
    return true // use debug
  else
    return false // use loggly for LOG and below
}

const context = 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