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

@studiowebux/logger

v6.0.0-alpha

Published

A wrapper to log everything with winston and morgan.

Downloads

3

Readme

Introduction

This module uses morgan & winston.
These two features are implemented

  • A custom logger function
  • A request interceptor

Why using this module,

  • It allows to redirect the logs in files, on the console and/or in logstash.
  • It allows to collect the request content easily with filters.

For more details (EN/FR) : Wiki

Installation

npm install --save @studiowebux/logger

NPM

Usage

Configuration

Options

| Key | Value | Description | | -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | | type | (Morgan) - Possible choices : [combined, tiny, dev, common, short, json] | The format options is only used when the type is set to json | | tokens | (Morgan) - A list of tokens to collect information from. | If set to null, the default tokens will be used, See below for the default values. | | format | (Morgan) - The keys to collect in the request. | only used when the type is set to json | | application_id | (Winston) - An ID to simplify tthe sorting of the information. |   | | forceConsole | (Winston) - A boolean to print the messages on the console even in production. | By default, the console is deactivated in production mode. | | consoleLevel | (Winston) - It sets the log level to print on the console, Possible choices : [error, warn, info, verbose, debug, silly] | If silly is chosen, all levels will be printed, but choosing error will only print the errors. | | logstash | (Winston) - The logstash configuration. | An ELK instance is required to use this option Only the UDP configuration is supported. | | filenames | (Winston) - A list of log level to be redirected in file. | | | deniedKeys | (Winston) - A list of values that will be replaced with '*****' | See the examples for more information |

Available options:

const options = {
  type: 'json', // combined, tiny, dev, common, short, json
  tokens: null,
  format: {
    method: ':method',
    url: ':url',
    status: ':status',
    body: ':body',
    params: ':params',
    query: ':query',
    headers: ':headers',
    'http-version': ':http-version',
    'remote-ip': ':remote-addr',
    'remote-user': ':remote-user',
    length: ':res[content-length]',
    referrer: ':referrer',
    'user-agent': ':user-agent',
    'accept-language': ':language',
    'response-time': ':response-time ms',
  },
  application_id: 'Test01',
  forceConsole: false,
  consoleLevel: 'silly', // error, warn, info, verbose, debug, silly
  logstash: {
    host: '127.0.0.1',
    port: '50000',
    mode: 'tcp',
  },
  filenames: {
    error: 'log/error.log',
    warn: 'log/warn.log',
    info: 'log/info.log',
    verbose: 'log/verbose.log',
    debug: 'log/debug.log',
    silly: 'log/silly.log',
  },
  deniedKeys: ['password', 'authorization', 'accessToken', 'refreshToken'],
};

Environment Variables

| Name | Values | | --------------------- | ---------------------------------------- | | LOGGER_CONSOLE_LEVEL | error, warn, info, verbose, debug, silly | | LOGGER_FORCE_CONSOLE | true or false | | LOGGER_APPLICATION_ID | |

Default tokens:

module.exports = [
  {
    name: 'body',
    needStringify: true,
  },
  {
    name: 'params',
    needStringify: true,
  },
  {
    name: 'query',
    needStringify: true,
  },
  {
    name: 'headers',
    needStringify: true,
  },
  {
    name: 'type',
    needStringify: false,
    value: 'content-type',
    parent: 'headers',
  },
  {
    name: 'language',
    needStringify: false,
    value: 'accept-language',
    parent: 'headers',
  },
];

Functions

constructor(opts = {}, log = console)

Initializes the configuration and the default logger.

const WebuxLogger = require('@studiowebux/logger');

const webuxLogger = new WebuxLogger(opts, console);

CreateLogger(): Object

It attaches the custom logger to the log variable.
It also returns the logger function.

const log = webuxLogger.CreateLogger();

To use the custom logger function:

Both methods are equivalent

log.info('...');
log.error('...');
log.warn('...');
log.verbose('...');
log.debug('...');
log.silly('...');

webuxLogger.log.info('...');
webuxLogger.log.error('...');
webuxLogger.log.warn('...');
webuxLogger.log.verbose('...');
webuxLogger.log.debug('...');
webuxLogger.log.silly('...');

OnRequest(): Function

It configures the request interceptor.

this is required to have an Express instance to use the app.use function.

const express = require('express');
const app = express();

const webuxLogger = new WebuxLogger(options, console);

app.use(webuxLogger.OnRequest());

Quick start

The /examples directory has multiple use cases.

The request interceptor with Morgan

index.js

const WebuxLogger = require('@studiowebux/logger');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const options = {
  type: 'json',
  format: {
    method: ':method',
    url: ':url',
    status: ':status',
    body: ':body',
    params: ':params',
    query: ':query',
    headers: ':headers',
    'http-version': ':http-version',
    'remote-ip': ':remote-addr',
    'remote-user': ':remote-user',
    length: ':res[content-length]',
    referrer: ':referrer',
    'user-agent': ':user-agent',
    'accept-language': ':language',
    'response-time': ':response-time ms',
  },
};

const webuxLogger = new WebuxLogger(options, console);

app.use(webuxLogger.OnRequest());

app.use(
  bodyParser.json({
    limit: '10MB',
  }),
);

app.get('/wait', (req, res) => {
  setTimeout(() => {
    res.status(200).json({ message: 'it took 1.5 seconds ...' });
  }, 1500);
});

app.use('*', (req, res) => {
  res.send('BONJOUR !');
});

app.listen(1337, () => {
  webuxLogger.log.info('Server is listening on port 1337');
});
  • This configuration print the messages on the console without winston.
  • The requests are logged in JSON format without filters, that means that everything is logged (this is unsecure to do that).

(Secure way) Add filters provided by the custom logger (CreateLogger()).

** To log the body content, you have to use the body-parser package.

ELK (Elastic, Logstash & Kibana)

The examples directory has a Docker to start an ELK instance to do some tests.

docker-compose up -d

Videos and other resources

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

SEE LICENSE IN license.txt