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

@bedrockio/logger

v1.1.0

Published

Structured logger for Bedrock applications.

Downloads

632

Readme

@bedrockio/logger

Structured logger that targets both the console and remote cloud formats. This includes:

  • Pretty formatting for the console.
  • Request logging middleware.
  • Google Cloud structured logger.
  • Google Cloud batched tracing via OpenTelemetry.

Install

yarn install @bedrockio/logger

Usage

const logger = require('@bedrockio/logger');
logger.setupGoogleCloud({
  // Set up gcloud structured logging. Default true.
  logging: true,
  // Set up gcloud tracing. Default true.
  tracing: true,
});

This initialization code should be added as early as possible in your application.

Options

Enable both logging and tracing and tell the tracing to ignore specific paths.

const logger = require('@bedrockio/logger');
logger.setupGoogleCloud({
  tracing: {
    ignoreIncomingPaths: ['/'],
  },
});

Log Levels

In development, setting process.env.LOG_LEVEL will set the log level which silences lower level output:

  • debug
  • info
  • warn
  • error

The default is info which silences debug level logs.

Methods

logger.useConsole

Sets the logger to use console output for development. This is the default.

logger.useGoogleCloud

Sets the logger to output structured logs in JSON format. Accepts an options object:

  • getTracePayload - This connects the logger to tracing, allowing you to batch logs by requests.

logger.useGoogleCloudTracing

Enables batched Google Cloud tracing for Koa and Mongoose. This will allow discovery of slow operations in your application. The Cloud Trace API must be enabled to use this.

Logger Methods

logger.debug('Hello');
logger.info('Hello');
logger.warn('Hello');
logger.error('Hello');

The basic methods will output logs at different levels.

Object Logging

logger.info({
  foo: 'bar',
});

Passing an object into the console logger will output it as you would see in the console. When using the Google Cloud logger it will output a structured JSON payload that allows inspecting of the object in the logging console.

Multiple Arguments

logger.info('foo', 'bar');
logger.info(obj1, obj2);

Multiple arguments will be concatenated together in the console logger. The Google Cloud logger will present a truncated message and export complex objects to the JSON payload.

String Formatting

logger.info('%s -> %s', 'foo', 'bar'); // foo -> bar

Basic printf style formatting is supported out of the box by the console logger, and the Google Cloud console will format basic tokens (%s, %d, and %i). Note that decimal precision formatting such as "%.2d" is not supported.

logger.middleware

Koa middleware that logs HTTP requests:

const Koa = require('koa');
const logger = require('@bedrockio/logging');

const app = new Koa();
app.use(logger.middleware());

Recording Request Body

The logger middleware allows the request body to be recorded in the logs. This must be opted into:

app.use(logger.middleware({
   recordParams: [
    {
      // Record all params for this route with status >= 400
      path: '/1/foo',
    },
    {
      // Record all params for this route with status >= 200
      status: 200,
      path: '/1/foo',
    },
    {
      // Record only POST requests
      method: 'POST',
      path: '/1/foo',
    },
    {
      // Match path by regex
      path: /\/1\//foo/,
    },
    {
      // Include only specific params
      include: ['shop']
    },
    {
      // Include all but excluded params
      exclude: ['shop']
    },
    {
      // Params also support regexes
      include: [/shop/]
    },
  ],
}));

Passing true for the flag here will enable for all requests.

// Records all request >= (over status 400 by default)
app.use(logger.middleware({
   recordParams: true,
}));