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.
npm install logfmtVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic `stringify` and `parse` operations, then shows how to use `streamParser` to process logfmt data from a simulated stream.
Consider alternatives for structured logging like `pino`, `winston`, or `bunyan` which offer active maintenance and modern features. If logfmt format is strictly required, fork the repository or use a more actively maintained logfmt parser/stringifier if one exists.
Manually cast values to numbers after parsing if numeric operations are required: `const parsed = logfmt.parse('count=123'); const count = parseInt(parsed.count, 10);`To create an independent, configurable instance, use `const customLogfmt = new logfmt();`. This will allow you to modify `customLogfmt` without affecting the main singleton instance.
For Node.js projects, use `const logfmt = require('logfmt');`. If an ESM-only project needs this, consider a wrapper or transpilation, but be aware of potential issues due to its age and CJS-centric design.Ensure `const logfmt = require('logfmt');` is correctly placed and executed before calling any methods on `logfmt`. Verify no other code is inadvertently overwriting the `logfmt` variable.Check that the stream being piped from (e.g., `process.stdin`, `req`, a `Readable` stream) is properly initialized and available. Ensure `logfmt.streamParser()` or `logfmt.streamStringify()` are correctly called without errors.
Use `logfmt.parse(yourLogfmtString)` to correctly parse logfmt strings into JavaScript objects. Reserve `JSON.parse()` for actual JSON strings.
Add `const logfmt = require('logfmt');` at the top of your file to properly import the module.No dependency data recorded yet.