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

@anandsuresh/smart-log

v1.0.2

Published

A smart log-agent and log-sinks

Downloads

15

Readme

smart-log

node (scoped) npm (scoped) npm Travis license GitHub followers Twitter Follow

The smart-log module provides a set of modules to log information to one or more log sinks. It consists of a singleton log agent implemented as a Readable object stream and exposes logging/metrics methods. The agent can be piped into one or more Writable log sinks including:

  • ConsoleSink: writes the process.stderr
  • SyslogSink: writes to the specified syslog server
  • RotatingFileSink: writes to a local file and rotates it at the start of each new day

usage

const SmartLog = require('@anandsuresh/smart-log')

// Initialize the log agent to:
// - log things at info and lower
// - log the process pid in each log message
// - queue up to 1000 of the latest log items if the sink(s) is/are slow
const log = SmartLog.init({
  level: 'info',
  default: { pid: process.pid },
  queue: { strategy: 'overwrite', size: 1000 }
})

// Log everything from the agent to stderr
const stderr = SmartLog.createConsoleSink()

// Log notice and above to a file
const rotatingFile = SmartLog.createRotatingFile({
  filter: log => ~['debug', 'info', 'notice'].indexOf(log.level),
  path: '/var/log', // make sure your process has write privileges at this path
  prefix: 'log-',   // log file name prefix; date will be appended to this
  suffix: '.log'    // log file name suffix; will be appended to the end of the file name
})

// Only log warning and below to syslog
const syslog = SmartLog.createSyslogSink({
  filter: log => ~['warning', 'error', 'critical', 'alert', 'emergency'].indexOf(log.level),
  id: process.pid,        // unique name reported to syslog
  hostname: '127.0.0.1',  // hostname/IP address of the syslog daemon
  port: 514,              // TCP port of the syslog daemon
  facility: 'user',       // syslog facility to log to
  fqdn: os.hostname()     // hostname of the system; logged in syslog header
})

// Pipe the log agent into the sinks
log.pipe(stderr)
log.pipe(rotatingFile)
log.pipe(syslog)

// Use the log agent
log.emergency('this will be logged to stderr and syslog')
log.alert('this will be logged to stderr and syslog')
log.critical('this will be logged to stderr and syslog')
log.error('this will be logged to stderr and rotating file')
log.warning('this will be logged to stderr and rotating file')
log.notice('this will be logged only to stderr and rotating file')
log.info('this will be logged only to stderr and rotating file')
log.debug('this will be logged only to stderr')

agent

The log agent is a Readable object-mode stream with logging/metrics methods attached and is provided by the module as a singleton.

The log levels are taken from Syslog and include:

  • emergency: conditions requiring immediate operator intervention to proceed (e.g. out-of-memory)
  • alert: conditions requiring immediate operator intervention (e.g. high cpu usage)
  • critical: conditions indicating critical process state (e.g. low disk space)
  • error: error conditions
  • warning: warning conditions
  • notice: normal but significant conditions
  • info: informational logging
  • debug: debug logging

The metrics methods include:

  • counter: records the count of values
  • histogram: records the distribution of values

sinks

Sinks are Writable object-mode streams that filter log messages and write them to the specified destination. The sinks provided with this module include:

ConsoleSink

The console sink writes JSON-serialized logs to the process' stderr stream.

RotatingFileSink

The rotating file sink writes JSON-serialized logs to a file that is rotated every 24 hours. The timestamp from the log message is used to identify when files need to be rotated.

SyslogSink

The syslog sink writes JSON-serialized logs to the specified syslog server. Since the messages are in JSON format, the syslog server MUST use the Common Event Expression module.