JSON Log Line Utility

1.1.3 · active · verified Sun Apr 19

The `json-log-line` package, currently at version 1.1.3, provides a focused utility for transforming JSON and plain JavaScript objects into custom-formatted log line strings. It is designed to integrate seamlessly with newline-delimited (nd) JSON loggers, offering flexible control over log output. Key functionalities include robust support for both JSON and standard JavaScript objects, a configurable system for explicitly including or excluding specific fields, and an advanced `format` option. This `format` option allows users to define custom transformations for object keys, including nested properties via dot notation, and supports sophisticated multi-key formatting strategies such as "take one" (`|`) or "take all" (`,`) for conditional or sequential application of formatters. The library ships with TypeScript types, ensuring a good development experience for TypeScript users. Releases are made periodically, indicating active maintenance and continuous refinement of its features.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize `logLineFactory` with include/exclude/format options, including multi-key formatting, and process JSON strings into custom-formatted log lines.

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`,
    // Example of 'take one' multi-key formatter
    "foo|baz": (value) => `!${value}`,
    // Example of 'take all' multi-key formatter
    "nested.a,other.a": (value) => `:${value}:`,
  },
};

const lineFormatter = logLineFactory(options);

const log1 = JSON.stringify({
  nested: {
    other: "something",
    field: "FIELD",
    a: "x",
    b: "y"
  },
  part1: "hello",
  part2: "world",
  some: "extra",
  data: "here",
  foo: "bar",
  baz: "buz",
  other: { a: "z" }
});

console.log(lineFormatter(log1));
// Expected output (may vary slightly based on unformatted fields order):
// [hello] [world] :FIELD:
// !bar :x: :z:
// {"nested":{"b":"y"},"some":"extra","data":"here","baz":"buz","other":{}}

const log2 = JSON.stringify({
    message: "User logged in",
    level: "info",
    timestamp: new Date().toISOString(),
    userId: 123,
    ipAddress: "192.168.1.1",
});

const simpleFormatter = logLineFactory({
    format: {
        level: (value) => `[${value.toUpperCase()}]`,
        message: (value) => `${value}`,
        timestamp: (value) => `(${new Date(value).toLocaleTimeString()})`,
        extraFields: (value) => JSON.stringify(value)
    }
});

console.log(simpleFormatter(log2));
// Example Output: [INFO] User logged in (12:17:00 PM) {"userId":123,"ipAddress":"192.168.1.1"}

view raw JSON →