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

kitten-logger

v0.3.2

Published

## Features + Cluster Mode + Log rotation + Custom formatters by namespaces + Support Debug library variables + Filtering at runtime

Downloads

29

Readme

kitten-logger

Features

  • Cluster Mode
  • Log rotation
  • Custom formatters by namespaces
  • Support Debug library variables
  • Filtering at runtime

Get started

Install

npm install kitten-logger

Log

const kittenLogger = require('kitten-logger');

kittenLogger.init();

let logger = kittenLogger.createPersistentLogger('http');

logger.info('My message to log');

Philosophy

A master process initializes kitten-logger (kittenLogger.init) to capture stdout and stderr from master process and forked processes if it is run in cluster mode. Then, it logs entries in log file in CSV format or directly in the terminal.

API

Log levels

  • DEBUG
  • INFO
  • WARN
  • ERROR

Environment variables

Variable | Description ------------------------------------|------------ KITTEN_LOGGER | Enable namespaces, ex: namespace:*. It supports multi-filters with , as separator, ex: namespace,otherNamespace. By default, the variable searchs DEBUG variable. If no DEBUG variable has been found, the * is set. KITTEN_LOGGER_LEVEL | Filter log level from specified level. Default is INFO. The hierarchy is DEBUG < INFO < WARN < ERROR. KITTEN_LOGGER_RETENTION_DAYS | Set log retention days. By default is 10; KITTEN_LOGGER_RETENTION_DIRECTORY | Directory to write to logs file. Default is logs. KITTEN_LOGGER_RETENTION_FILENAME | Filename where to write logs. Default is out; KITTEN_LOGGER_IS_LOADED | If true, kitten-logger will prefix all logs by K_LOG for sub instances of kitten-logger. KITTEN_LOGGER_DEST | If auto, kitten-logger will write logs to the terminal if the process process.stdout.fd is a TTY if not, kitten-logger will write logs to the out.log file | If tty, kitten-logger will write logs to the terminal | If file, kitten-logger will write logs to the out.log file

Initialisation kittenLogger.init

The initialisation function create the logs directory where the process has been launched. If it is in TTY mode, the logs are redirected to the terminal. Otherwise, the logs are writted to the file out.log. A rotation log system is enabled to rotate logs by day.

Loggers

kitten-logger differenciates two types of logger:

  • persitent logger
  • simple logger

kittenLogger.createPersistentLogger({String} namespace) -> {LoggerObject}

Create a persistent logger. It returns a logger Object.

Variable | Description ----------------|------------ namespace | Global name to identify the logger. This name is unique. It can be filter by the variable KITTEN_LOGGER.

Example

const kittenLogger = require('kitten-logger');

let logger = kittenLogger.createPersistentLogger('http');

logger.error(err);

kittenLogger.createLogger({String} namespace) -> {LoggerObject}

Create a simple logger. It returns a logger Object.

Variable | Description ----------------|------------ namespace | Name to identify the logger. It is not unique. It can be filter by the variable KITTEN_LOGGER as the creation of the logger.

Example

const kittenLogger = require('kitten-logger');

let logger = kittenLogger.createLogger('http');

logger.error(err);

LoggerObject

Methods

  • LoggerObject.debug({String/Object} msg, {Object} options): log given message with level DEBUG.
  • LoggerObject.info({String/Object} msg, {Object} options): log given message with level INFO.
  • LoggerObject.warn({String/Object} msg, {Object} options): log given message with level WARN.
  • LoggerObject.error({String/Object} msg, {Object} options): log given message with level ERROR.
  • LoggerObject.extend({String} namespace) -> {LoggerObject}: extend the current logger, the namespace of the final LoggerObject will be the concatenation of the current one with the given parameter as parentNamespace:childNamespace. options is an Object, the key isKittenLogger can be set to follow logs through a chain of actions.

Terminal formatters

When displaying logs in terminal, formatters can be applied by namespaces.

kittenLogger.addFormatter({String} namespace, {Function} formatterFn)

Variable | Description ----------------|------------ namespace | Namespace of a logger to format. formatterFn | Function to format message before logging in terminal.

Example

const kittenLogger = require('kitten-logger');

let logger = kittenLogger.createLogger('http');

kittenLogger.addFormatter('http', msg => {
  return msg.method + ' ' + msg.url;
});

...

// Logger, log the `req` object of an HTTP request
logger.log({ method : req.method, url : req.url });

kittenLogger.formattersCollection -> Object

Return a list of predefined formatters (/lib):

  • http_start, log HTTP request
  • http_end, log HTTP request's result

Filter

Kitten-logger allows you to filter namespaces & levels at runtime.

Static

Define the variable KITTEN_LOGGER.

process.env.KITTEN_LOGGER = 'onlyMyNamespace';

Define the variable KITTEN_LOGGER_LEVEL.

process.env.KITTEN_LOGGER_LEVEL = 'INFO';

Programatic

kittenLogger.filter({String} filter)

kittenLogger.filter('onlyMyNamespace');

kittenLogger.filterLevel({String} level)

kittenLogger.filterLevel('INFO');

Receive actions from outside the process

kitten-logger is allow to communicate between its pairs if exist in sub dependencies via socket.

To send action to kittenLogger instances, a process must connect to the given path, ex: /tmp/kitten_logger_<inode>. Where <inode> is the file descriptor's value of the master process.

The message must be of JSON type aand follow the given format :

{
  action : <ACTION>,
  value  : <value>
}

Where <ACTION> is a string, and <value> is the value for the action.

Actions | Description --------------|------------ FILTER | Call the mehod kittenLogger.filter for the clients connected to the socket server. FILTER_LEVEL| Call the mehod kittenLogger.filterLevel for the clients connected to the socket server.

kittenLogger.listen()

Start a socket server. It must be run once in the master process.

kittenLogger.connect()

Connect to the socket server. It must be run once in the child process. If will attemp to connect to the socket server for 20 seconds.