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

sw-node-logger

v1.0.3

Published

An easy and modulable logger for nodejs

Downloads

11

Readme

sw-node-logger

An easy to use logger for node js

Quick start

let Logger = require('sw-node-logger')

// Instantiate a Logger
let logger = new Logger([
  // Instantiate the driver(s) you want to use
  new Logger.DRIVERS.CONSOLE(),
  new Logger.DRIVERS.FILEROLLING({ logDirectory: './log' })
])

logger.Log('This is info')

Documentation

How to log

When the logger is instantiated, you simply need to call the Log method. This will Log on all defined Drivers. By default the log type is INFO but you can either change the default log type or specify it for a specific log

// Default log type is INFO
logger.Log('This is info')

// Set default to WARNING
logger.SetType(Logger.TYPES.WARN)
logger.Log('This is warn')

// Specify the type for this log
logger.Log('This is error', Logger.TYPES.ERROR)

What is logged

The log types are organized by priority

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

By default the lvl is set to INFO. It means that everything above will be logged but not what is below. You can change the default lvl with this command.

// Set the minimum log lvl to display to WARNING
logger.SetLvl(Logger.TYPES.WARN)

The drivers

This library use drivers. You can specify one or many.

// How to set options
let options = {
  useColors: false
}

let drv = new Logger.DRIVERS.CONSOLE(options)

// Give that driver to the main Logger object
let logger = new Logger(drv)

Console Driver

This will simply log into the console

| Option | Type | Default | Description | |--------|-------|---------|-------------| | timestampFormat | string | 'YYYY-MM-DD HH:mm:ss' | DateTime format for the log line | | useColors | boolean | true | Use colors in the console |

File rolling Driver

This will log into a file. A new file everyday

| Option | Type | Default | Description | |--------|-------|---------|-------------| | timestampFormat | string | 'YYYY-MM-DD HH:mm:ss' | DateTime format for the log line | | logDirectory | string | './' | The directory where to store the logs | | fileNamePattern | string | 'log-<DATE>.log' | The filename format. <DATE> will be automatically replaced by the date of the day | | keepLast | integer | 10 | How many log file to keep. If it set to 0, all the files will be kept |

Syslog Driver

This will log into a syslog server

| Option | Type | Default | Description | |--------|-------|---------|-------------| | timestampFormat | string | 'YYYY-MM-DD HH:mm:ss' | DateTime format for the log line | | srvAddress | string | 'localhost' | The address of the server | | port | integer | 514 | The server port number | | transport | 'TCP' or 'UDP' | 'UDP' | The transport type | | hostName | string | computer hostname | The hostname of your machine displayed on the syslog | | appName | string | 'MyApp' | The application name |

Custom Driver

How to build your custom driver

The minimum required is to use a constructor with options and a Log method which get message and type

let Logger = require('sw-node-logger')

class MyDriver extends Logger.LoggerDriverInterface {
  constructor (options) {
    super(options)
  }

  Log (message, type) {
    console.log(message, type)
    // You can use the common formatting method
    // to get a clean line
    console.log(this.Format(message, type))
  }
}

let logger = new Logger([
  new MyDriver()
])

logger.Log('Voilà', Logger.TYPES.ERROR)