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

tracing-logger

v1.0.7

Published

simple logging and tracing

Downloads

370

Readme

tracing-logger

The tracing-logger is an opinionated implementation of the winston logger that simplifies tracing and correlation of log messages within a single application, and across services.

When used across multiple services, you can also enjoy a consistent log format making it more straightforward to create search facets in your log tooling.

The log level is set to info where NODE_ENV is production, and debug otherwise.

Log Format

Each log message is created with a UTC timestamp and a tracing node. This will contain an x-correlation-id uuid at a minimum in addition to any other values you choose to add.

{
  "timestamp":"2020-06-22T21:13:36.105Z",
  "tracing":{
    "x-correlation-id":"a71cabcc-7f89-451b-b72c-baeee9f3c7c5",
    "user-id":"38e2c385-bd20-4e98-91b7-930d22959989"
  },
  "message":"some log message or data",
  "level":"debug"
}

The logger should be initialised and added as middleware to your server to allow it to intercept each incoming request.

The x-correlation-id attribute is added automatically when the logger is initialised.
If this header is provided then the consumer provided value will be used, otherwise a new uuid will be generated and added.

Additional values can be appended to the tracing object for automatic inclusion on all log messages.

Initialisation

const express = require('express')

// Import the tracing-logger package
const logger = require('tracing-logger')

const app = express()
const port = 3000

// Initialise the logger, this will create a tracing namespace that
// is unique to the current request.  If that request contains an
// `x-correlation-id` header the value will be added to the tracing
// object on the logger, otherwise a new uuid will be generated and used.
app.use(logger.init());

app.get('/', (req, res) => {
  logger.info("x-correlation-id attached")

  // Further tracing values can be added as required
  logger.tracing.set('service name', 'express example')
  logger.info("service name attached")

  // All tracing values can be accessed at once,
  // this is useful when passing values to
  // downstream services
  res.send(logger.tracing.getAll())
})

app.listen(port, () => logger.info(`Example app listening at http://localhost:${port}`))

Usage

// The logger can be imported in any file within
// the service, it will still have access to the
// same tracing values which will be automatically
// included on any logs
const logger = require('tracing-logger')

const books = [
  {
    title: 'Wuthering Heights',
    author: 'Emily Brontë',
  },
  {
    title: 'Jurassic Park',
    author: 'Michael Crichton',
  },
];

const fetchAll = () => {
  logger.debug(`Fetching All Of The Data: ${JSON.stringify(books)}`);
  return books;
};

const callDownstream = () => {
  const xCorrelationId = logger.tracing.get('x-correlation-id');

  // attach the xCorrelationId to the
  // `x-correlation-id` request header
  // when making downstream requests
};

module.exports = { fetchAll };

See the apollo-server-express example for a working demonstration.