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

@saramorillon/logger

v1.3.0

Published

0 dependencies stdout logger, with actions, meta and parsers.

Downloads

12

Readme

Logger

0 dependencies stdout logger, with actions, meta and parsers.

Table of contents

Quick Start

First, install the logger using your favourite package manager:

# npm
npm install --save @saramorillon/logger
# yarn
yarn add @saramorillon/logger

You can now create a new instance of logger:

import { Logger, ILoggerOptions } from '@saramorillon/logger'

const options: ILoggerOptions = {
  silent: false,
}

const metadata: Record<string, unknown> = {
  loggerId: 'my-awesome-logger',
}

const logger = new Logger(options, metadata)

logger.info('This is a good day!')

This code will produce:

{
    "timestamp": <current date>,
    "level": "info",
    "message": "This is a good day!",
    "loggerId": "my-awesome-logger"
}

Note that metadata are optional.

Options

| Name | Default value | Mandatory | Description | | ------ | ------------- | --------- | ----------------------------------------------- | | silent | false | No | Set to true if you want the logger to be silent |

About metadata

  • Metadata are optional
  • You can put everything you want in metadata
  • Metadata will be logged with every log message
  • Metadata can be formatted using parsers

About parsers

When logging properties in metadata, sometimes you don't want to log full payload but only a subset of properties. For that, you can use parsers. You can define a parser using the setParser method. A parser is a function associated to a property name. When logging, when the logger encounter a metadata with that name, it will parse the value.

Example

import { Logger, ILoggerOptions } from '@saramorillon/logger'

const fullObject = {
  prop1: 'value1',
  prop2: 'value2',
  prop3: 'value3',
}

const logger = new Logger()
logger.addMeta(fullObject)
logger.info('Log without parser') // 1

function parser(meta: unknown): Record<string, unknown> | undefined {
  if (typeof obj === 'object' && obj !== null) {
    return meta.prop1
  }
}
logger.setParser('fullObject', parser)
logger.info('Log with parser') // 2
  1. This will produce a log with the full metadata payload:
{
    "timestamp": <current date>,
    "level": "info",
    "message": "Log without parser",
    "prop1": "value1",
    "prop2": "value2",
    "prop3": "value3"
}
  1. This will produce a log with parsed metadata payload:
{
    "timestamp": <current date>,
    "level": "info",
    "message": "Log with parser",
    "prop1": "value1"
}

About actions

An action is a set of logs starting with a starting log and ending with a success log or a failure log, depending on the result. This logger provides an easy way to describe actions sharing the same metadata and a unique action ID.

Example

import { Logger, ILoggerOptions } from '@saramorillon/logger'

const logger = new Logger()

let action = logger.action('first_action') // 1
try {
  // Do nothing
  action.success() // 2
} catch (error) {
  action.failure(error) // 3
}

action = logger.action('second_action') // 4
try {
  throw new Error('This is an error')
  action.success() // 5
} catch (error) {
  action.failure(error) // 6
}
  1. This will produce the first action starting log:
{
    "timestamp": <current date>,
    "level": "info",
    "message": "first_action"
}
  1. This will produce the first action success log:
{
    "timestamp": <current date>,
    "level": "info",
    "message": "first_action_success"
}
  1. This will produce nothing.

  2. This will produce the second action starting log:

{
    "timestamp": <current date>,
    "level": "info",
    "message": "second_action"
}
  1. This will produce nothing.

  2. This will produce the second action failure log:

{
    "timestamp": <current date>,
    "level": "error",
    "message": "second_action_failure",
    "error": {} // Not that without an appropriate parser, errors will be stringified as an empty object
}

API

| Method | Description | | ------------------------------------------------------------------- | ------------------------------------------- | | addMeta(meta2: Record<string, unknown>): Logger | Add a new property to logger metadata | | setParser(name: string, parser: Parser): Logger | Add a new parser for specific metadata name | | action(message: string, meta2?: Record<string, unknown>): IAction | Start a new action | | info(message: string, meta2?: Record<string, unknown>): Logger | Log using info level | | warn(message: string, meta2?: Record<string, unknown>): Logger | Log using warn level | | error(message: string, meta2?: Record<string, unknown>): Logger | Log using error level |