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

lambert-logger

v0.2.1

Published

A simple configuration tool for the wonderful [winston logger](https://github.com/winstonjs/winston). Formats and Optimizes for AWS lambda log writing.

Downloads

2

Readme

lambert-logger

A simple configuration tool for the wonderful winston logger. Formats and Optimizes for AWS lambda log writing.

Table of Contents

Usage

// src/logging.js
import Lambert from 'lambert-logger';

const { getLogger } = Lambert.create({ name: "My Lambda" });

export default getLogger;

// src/my-module.js
import getLogger from './logging';

const logger = getLogger();

logger.info('Hello, World!'); // [info] My Lambda >> Hello, World!

Logging

Creating a Logger Factory

Before you can begin logging, you must first create a new Logger factory function for your application:

import Lambert from 'lambert-logger';

const { getLogger } = Lambert.create(config); // config is optional; see customization

Use the factory function to generate logger instances, and use those instances to generate log messages.

// method 1: export factory to import in other modules
export default getLogger;

// method 2: export a default logger instance
const instance = getLogger(config); // config is optional; see customization
export default instance;

Logging Levels

Logging levels conform to the severity ordering specified by RFC5424: severity of all levels is assumed to be numerically ascending from most important to least important.

Each level is given a specific integer priority. The higher the priority the more important the message is considered to be, and the lower the corresponding integer priority. As specified exactly in RFC5424 the logger levels are prioritized from 0 to 6 (highest to lowest):

{
  error: 0,   // critical errors that cause the process to fail
  warn: 1,    // errors that are recoverable but need to be documented
  info: 2,    // standard informational log; relevant to business domain
  verbose: 3, // noisy version of info; print object properties, configs, etc.
  debug: 4,   // information to help troubleshoot issues; ex. error stack traces
  silly: 5    // trace level information, should probably be removed before merge into dev/prod
}

Setting the level for your logging message can be accomplished by using the level specified methods defined on every logger instance.

logger.info("127.0.0.1 - there's no place like home");
logger.warn("127.0.0.1 - there's no place like home");
logger.error("127.0.0.1 - there's no place like home");

Out of the box, the logger library filters log messages by level. The runtime environment and NODE_ENV variable determine the target transport:

| environment | minimum log level | running on AWS? | node env | | ----------- | ----------------- | --------------- | -------- | | Local (default) | silly | no | * | | AWS Production | success | yes | production | | AWS Development | debug | yes | * |

AWS lambda NodeJS runtimes do not define NODE_ENV automatically. The environment must be set via lambda configuration or otherwise.

Configuration

Factory Configuration

Customize the logging factory by passing in an object to the create function with the following properties:

  • name: Name to prepend to each log statement. Defaults to Service.
  • defaultMeta: Object containing arbitrary information to include along every log message. See winston documentation for details.
  • testLevel: Determines the lowest priority allowed during a local or CI test run. Defaults to 'error'.
  • delimiter: Delimiter between the name and the log message. Defaults to >>.
  • transforms: Functions to format log statements before they are written. See User Transforms section for details.
  • transformOpts: Custom options accessible to all user transforms in the transforms list when provoked.
  • hooks: Custom hooks used to extend or override base container functionality. Advanced use only.

Instance Configuration

Likewise, logger instances can be configured by passing in an object to the factory function with the following properties:

  • name: Optional name of the logger instance. Used to identify log messages generated from specific modules in your application.

    const { getLogger } = Lambert.create({ name: "My Lambda" });
    const log = getLogger({ name: "Submodule" });
    
    // [info] My Lambda >> Submodule >> All systems go!
    log.info("All systems go!");

User Transforms

Transform functions allow you to transform log messages before they are written to the stream. You can add transforms by passing in a list to the factory configuration:

const lazyLogTransform = () => {
  //
  // user transform parameters:
  // - info: winston log transformable info
  // - opts: object containing helper functions and global transform options
  //
  return (info, opts) => {
    // extract message and splat from info object
    const { message } = opts.unpack(info);
    if (_isFunction(message)) {
      const [format, ...rest] = message();
      // update info object with message and splat arguments
      opts.pack(info, { message: format, splat: rest });
    }
    return info;
  };
};

const { getLogger } = Lambert.create({ name: "Example", transforms: [lazyLogTransform()] });
const logger = getLogger();

// [info] Example >> Payload info >> { ... }
logger.info(() => ['Payload info', getPayload()]);

Development

  • The library uses TSDX to manage configuration, builds, and publishing. If you plan on contributing as a developer, check out TSDX's documentation here.

Commands

To perform a live build, use:

npm start # or yarn start

This builds to /dist and runs the project in watch mode so any edits you save inside src causes a rebuild to /dist.

To do a one-off build, use npm run build or yarn build.

To run tests, use npm test or yarn test.

Jest

Jest tests are set up to run with npm test or yarn test.