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

@logzio-node-toolbox/logger

v1.1.0

Published

multi-functional logger

Downloads

18,586

Readme

Logger

lightweight logger base on the concept of transports and formatters.

Transports - logs destination (console, s3 etc...)

Formatters - mappers function before logs moved to the transport

logging methods

| method | weight | color | enum | | --- | --- | --- | --- | | silent | 0 | - | SILENT | | error | 2 | red | ERROR | | warn | 4 | yellow | WARN | | info/log/beatify | 6 | white | INFO | (beatify - will print parsed colorful json ) | debug | 8 | magenta | DEBUG |

Usage

when creating an instance of logger u need to pass at least one transporter and 0+ formatters each log will pass through.

each transporter can receive many formatter that will be invoke after all the logger formatters ended.

import { Logger, ConsoleTransport, formatters, LogLevel, addLogLevel, Color } from '@logzio-node-toolbox/logger';

const logFormatter = formatters.logSize();

const transportFormatter = formatters.omitFields(['name']);
const consoleTransport1 = new ConsoleTransport({name: 'console-1', formatters: [transportFormatter] });

const consoleTransport2 = new ConsoleTransport({name: 'console-2'});

const logger = new Logger({
  formatters: [logFormatter],
  transports: [consoleTransport1, consoleTransport2],
  metaData: { globalMetaData: 'globalMetaData' }
});

logger.info('hello', { name: 'from', last: 'last' });
// INFO: 18/07/2020 04:07:19.079 hello {"last":"last", logSize: 70 }
// INFO: 18/07/2020 04:07:19.079 hello {"last":"last", name: "from", logSize: 100 }

logger.removeTransport('console-1');

logger.info('hello', { name: 'name', message: 'override' }); // first args always message
// INFO: 18/07/2020 04:07:19.079 hello {"name":"from", logSize: 70 }

logger.info('hello', { name: 'from' }, {last: 'last'}); // concat objects args
// INFO: 18/07/2020 04:07:19.079 hello {"name":"from", last: 'last', logSize: 70 }

logger.info('hello', { name: 'from' }, [1,2,3]); // not objects will push to _logArgs_ array
// INFO: 18/07/2020 04:07:19.079 hello {"name":"from", _logArgs_: [[1,2,3]], logSize: 70 }

logger.info({ name: 'from', last: 'last' }); // first value can also be object
// INFO: 18/07/2020 04:07:19.079 {"name":"from", _logArgs_: [[1,2,3]], logSize: 70 }

logger.logLevel(LogLevel.DEBUG) // will start log debug as well

addLogLevel({name: 'RANDOM', color: Color.black, weight})

await logger.close() // will wait for all transports to close

Transports

ConsoleTransport

log to console

  • params
    • name - string (default console)
    • formatters - array | list of formatters
    • logLevel - string (default info) | representing the log level to log
    • color - boolean (default true) | adding color to output
import { Logger, ConsoleTransport } from '@logzio-node-toolbox/logger';
const consoleTransport = new ConsoleTransport({ name: 'logzioTransport', formatters: [transportFormatter], token:'123', meta: {region: 'prod', } });

LogzioTransport

send the log to logzio with the given token

  • params
    • name - string (default logzio)
    • host - string
    • token - string
    • type - string (default node-js)
    • metaData - object | default data to add to each log
    • formatters - array | list of formatters
    • logLevel - string (default info) | representing the log level to log
    • moreOptions - object (default true) | options to pass to logzio-nodejs
import { Logger, LogzioTransport } from '@logzio-node-toolbox/logger';
const logzioTransport = new LogzioTransport({ name: 'new-console', formatters: [transportFormatter] });

custom Transport

creating a custom Transport

  • functions:
    • logLevel - change log level
    • close - stop receiving logs
    • open - start receiving logs
import { Logger, Transport, formatters } from '@logzio-node-toolbox/logger';

class CustomTransports extend Transport {
  constructor(name, formatters, logLevel, ...paramsUNeed) {
    super({ name, formatters, logLevel })
  }
  // formatted log will arrive to this log function, so here u can do what u want with the data
  logLevel(data) {
    // do whatever with the data
  }

  // this function will be called and await when closing the logger
  close()
}

const customTransports = new CustomTransports({name: 'myCustomTransports', formatters: [formatters.logSize(100)] })
const logger = new Logger({transports: [ customTransports ] })
  logger.info('hello')
  customTransports.loLevel('DEBUG')
  logger.debug('hello')

Formatters

omitFields

Array of fields by path string tp remove from the log.

import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.omitFields(['path.to.omit', 'path.to.omit2']);
const logger = new Logger({ formatters: [formatter] });
logger.log({
  path: {
    to :{
      omit: 'should omit',
      omit2: 'also should omit',
      omit3: 'should not omit'
    }
  }
})
// output: INFO: 18/07/2020 04:07:19.079 { path: { to : { omit3: should not omit } } }

handleError

  1. look for err || error || message. err fields make sure they are objects.
  2. serialize the error and make sure logLevel is error.
import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.handleError();
const logger = new Logger({ formatters: [formatter] });
const err = new Error('random error');
logger.log({
  err
})
// output: ERROR: 18/07/2020 04:07:19.079 random error { stack: .... , type: .... }

logSize

  1. add logSize field to the log.
  2. validate max log size with value pass in bytes.
import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.logSize(100);
const logger = new Logger({ formatters: [formatter] });
logger.log('message with log size', { field: 'random' });
// output: INFO: 18/07/2020 04:07:19.079 message with log size { logSize: 40 }

const formatter = formatters.logSize(30);
const logger = new Logger({ formatters: [formatter] });
logger.log('message with log size', { field: 'random' });
// output: INFO: 18/07/2020 04:07:19.079 Log exceeded the max bytes size {logObjectKeys: ['message', 'field'] maxLogSize: 30 }

maskFields

mask fields by the length was received with *

  • params:
    • list - {{ field: string, length?: number }}
    • length - number (default 7)
import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.maskFields([{ field: 'password', length: 6 }], 10);
const logger = new Logger({ formatters: [formatter] });
logger.log({
  field: 'random',
  password: '12345678',
  ip: '123.123.123.123',
});
// output: INFO: 18/07/2020 04:07:19.079 message with log size { field: 'random', password: '**345678', ip: '****23.123.123' }

pickFields

will omit all object property except the given array

  • params
    • name - string
    • list - array of strings
    • shouldFlat - will flat the omit fields to the root of the log | default true
import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.pickFields('req', ['port', 'host'], true);
const logger = new Logger({ formatters: [formatter] });
logger.info("incoming" ,{ req: { port: '3000', host: 'localhost', ip: "127.0.0.1" }});
// INFO: 18/07/2020 04:07:19.079 { "message":"incoming", "port": "3000", host: "localhost" }

const formatter = formatters.pickFields('req', ['port', 'host'], false);
const logger = new Logger({ formatters: [formatter] });
logger.info("incoming" ,{ req: { port: '3000', host: 'localhost', ip: "127.0.0.1" }});
// INFO: 18/07/2020 04:07:19.079 { "message":"incoming", req: { "port": "3000", host: "localhost" } }

removeCircularFields

iterate over the log and remove all circular fields

import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.removeCircularFields();
const logger = new Logger({ formatters: [formatter] });

const a = { name : 'name'};
a.b = a;
logger.info(a);
// INFO: 18/07/2020 04:07:19.079 {"name":"name", "b": [Circular] }

renameFields

rename fields from to path to path

import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.renameFields({ 'path.to.field.rename': 'name.to.field.newName' });
const logger = new Logger({ formatters: [formatter] });

logger.info({ path: { to : {field : { rename: "some value"}}}});
// INFO: 18/07/2020 04:07:19.079 { path: { to : {field : { newName: "some value" }}}}

sliceFields

  • params
    • list - array of paths to slice (if field is object will stringify it before slice)
    • size - size to slice
import { Logger, formatters } from '@logzio-node-toolbox/logger';

const formatter = formatters.sliceFields(['path.to.slice'], 10);
const logger = new Logger({ formatters: [formatter] });

logger.info({ path: { to : { slice : { "some value to slice if its to long" }}}});
// INFO: 18/07/2020 04:07:19.079 { path: { to : {slice: "some value to...", __overSizedField__: { 'path.to.slice' : 30 }}}}}

logger.info({ path: { to : { slice : { field1: 'first filed to slice', field2: "second filed to slice" }}}});
// INFO: 18/07/2020 04:07:19.079 { path: { to : {slice: field1: 'first filed to slice', field2: "second f...", __overSizedField__: { 'path.to.slice.field1' : 20, 'path.to.slice.field2': 20 }}}}}