logfmt: Key-Value Logger and Parser

1.4.0 · abandoned · verified Tue Apr 21

logfmt is a Node.js library for working with the key-value logging convention popularized by Heroku. It provides functionalities for both serializing JavaScript objects into logfmt strings (e.g., `foo=bar a=14 baz="hello kitty"`) and parsing logfmt strings back into objects. The library also includes streaming capabilities for processing logfmt data from sources like `stdin` or HTTP requests, making it suitable for creating logplex drains or general structured log consumption/production. Currently at version 1.4.0, the package has not seen active development or releases since 2018, indicating an abandoned status. Its key differentiator is its direct support for the logfmt format, unlike general-purpose JSON loggers, making it specific to ecosystems leveraging this convention.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic `stringify` and `parse` operations, then shows how to use `streamParser` to process logfmt data from a simulated stream.

const logfmt = require('logfmt');
const through = require('through'); // often used for stream manipulation

// --- Non-streaming usage ---
const data = { status: 200, method: 'GET', path: '/api/items', duration_ms: 50 };
const logString = logfmt.stringify(data);
console.log('Stringified:', logString);
// Expected: Stringified: status=200 method=GET path=/api/items duration_ms=50

const parsedObject = logfmt.parse('status=200 method=GET path=/api/items duration_ms=50');
console.log('Parsed:', parsedObject);
// Expected: Parsed: { status: '200', method: 'GET', path: '/api/items', duration_ms: '50' }

// --- Streaming usage (parsing logfmt from a simulated input) ---
const { Readable } = require('stream');
const simulatedInput = new Readable({
  read() {
    this.push('metric=cpu_usage value=0.5 host=web-01\n');
    this.push('metric=memory_free value=1024 host=db-01\n');
    this.push(null); // No more data
  }
});

console.log('\n--- Streaming Parse Output ---');
simulatedInput
  .pipe(logfmt.streamParser())
  .pipe(through(function(obj) {
    console.log('Streamed Object:', obj);
  }));
// Expected: Streamed Object: { metric: 'cpu_usage', value: '0.5', host: 'web-01' }
// Expected: Streamed Object: { metric: 'memory_free', value: '1024', host: 'db-01' }

view raw JSON →