Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-log-line

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-log-line

A utility for building awesome log lines from JSON

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

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.

FAQs

Package last updated on 08 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc