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

json-log-line

v0.0.2

Published

A utility for building awesome log lines from JSON

Downloads

1,174

Readme

JSON Log Line Utility

This utility is designed to take JSON and plain objects and convert them into log line strings. It's compatible with any nd JSON logger.

Features

  • JSON and Plain Object Support: This utility can handle both JSON and plain objects, providing flexibility in the types of data you can convert into log line strings.

  • Compatible with any nd JSON Logger: This utility is designed to work with any nd JSON logger, making it a versatile tool for your logging needs.

Installation

To install this utility, use the following command:

npm install json-log-line

Usage

To use this utility, simply pass your JSON or plain object to the logLineFactory function:

const options = {
  /**
   * The key to use for the error object. Defaults to `err`.
   */
  errorKey: "err",
  /**
   * include and exclude both take keys with dot notation
   */
  exclude: [],
  /**
   * include always overrides exclude
   */
  include: [],
  /**
   * Format functions for any given key, keys of the object are automatically included and cannot be excluded
   * You can use dot notation as the object keys. The keys will print in order.
   */
  format: {
    some: (obj: any) => {
      return obj["some-key"].toString();
    },
    part: (obj: any) => {
      return obj["some-key"].toString();
    },
    of: (obj: any) => {
      return obj["some-key"].toString();
    },
    log: (obj: any) => {
      return obj["some-key"].toString();
    },
  },
};

export const lineFormatter = logLineFactory(yourObject);

This will return a formatter that can process strings or object output from any nd JSON logger.

example:

import { logLineFactory } from "json-log-line";

const options = {
  include: ["nested.field"],
  exclude: ["nested"],
  format: {
    part1: (value) => `[${value}]`,
    part2: (value) => `[${value}]`,
    "nested.field": (value) => `:${value}:\n`,
  },
};

const lineFormatter = logLineFactory(options);

const log = JSON.stringify({
  nested: {
    other: "something",
    field: "FIELD",
  },
  part1: "hello",
  part2: "world",
  some: "extra",
  data: "here",
});

console.log(lineFormatter(log));
// =>
// [hello] [world] :FIELD:
// {"some":"extra","data":"here"}

Often times you will want to stream nd json logs into a function that formats each log.

Options

format

Record<keyof parsed log object, () => string>

Format is an object the represents how you want to parse the log object. It will parse in natural order of the object.

format.extraFields

A special key that contains the rest of the log object fields which were both included and not formatted by a format function.

include

string[]

An array of object keys to include. Overrides excludes. All keys are included by default.

exclude

string[]

An array of object keys to exclude. The keys can be nested. Can be overridden with a more deeply nested include.