{"id":15691,"library":"logfmt","title":"logfmt: Key-Value Logger and Parser","description":"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.","status":"abandoned","version":"1.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/csquared/node-logfmt","tags":["javascript","log","parser","logfmt"],"install":[{"cmd":"npm install logfmt","lang":"bash","label":"npm"},{"cmd":"yarn add logfmt","lang":"bash","label":"yarn"},{"cmd":"pnpm add logfmt","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library is primarily CommonJS. While some bundlers may shim `import logfmt from 'logfmt'`, `require()` is the intended and most reliable way to import the module.","wrong":"import logfmt from 'logfmt';","symbol":"logfmt","correct":"const logfmt = require('logfmt');"},{"note":"Functions like `stringify` are properties of the main `logfmt` module object, not named exports for direct destructuring in CommonJS or ESM without specific configuration.","wrong":"import { stringify } from 'logfmt';","symbol":"logfmt.stringify","correct":"const logfmt = require('logfmt');\nlogfmt.stringify({ key: 'value' });"},{"note":"While `require('logfmt').parse` works, the module is designed as a singleton object, so accessing methods directly from the main `logfmt` constant is idiomatic.","wrong":"const parse = require('logfmt').parse;","symbol":"logfmt.parse","correct":"const logfmt = require('logfmt');\nlogfmt.parse('key=value');"},{"note":"Used for creating a readable stream that parses incoming logfmt lines into JavaScript objects. Requires piping from another stream.","symbol":"logfmt.streamParser","correct":"const logfmt = require('logfmt');\nprocess.stdin.pipe(logfmt.streamParser());"}],"quickstart":{"code":"const logfmt = require('logfmt');\nconst through = require('through'); // often used for stream manipulation\n\n// --- Non-streaming usage ---\nconst data = { status: 200, method: 'GET', path: '/api/items', duration_ms: 50 };\nconst logString = logfmt.stringify(data);\nconsole.log('Stringified:', logString);\n// Expected: Stringified: status=200 method=GET path=/api/items duration_ms=50\n\nconst parsedObject = logfmt.parse('status=200 method=GET path=/api/items duration_ms=50');\nconsole.log('Parsed:', parsedObject);\n// Expected: Parsed: { status: '200', method: 'GET', path: '/api/items', duration_ms: '50' }\n\n// --- Streaming usage (parsing logfmt from a simulated input) ---\nconst { Readable } = require('stream');\nconst simulatedInput = new Readable({\n  read() {\n    this.push('metric=cpu_usage value=0.5 host=web-01\\n');\n    this.push('metric=memory_free value=1024 host=db-01\\n');\n    this.push(null); // No more data\n  }\n});\n\nconsole.log('\\n--- Streaming Parse Output ---');\nsimulatedInput\n  .pipe(logfmt.streamParser())\n  .pipe(through(function(obj) {\n    console.log('Streamed Object:', obj);\n  }));\n// Expected: Streamed Object: { metric: 'cpu_usage', value: '0.5', host: 'web-01' }\n// Expected: Streamed Object: { metric: 'memory_free', value: '1024', host: 'db-01' }","lang":"javascript","description":"Demonstrates basic `stringify` and `parse` operations, then shows how to use `streamParser` to process logfmt data from a simulated stream."},"warnings":[{"fix":"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.","message":"The `logfmt` package appears to be abandoned. The last commit on GitHub was in June 2018, and the last release was in February 2018. This means there will be no further bug fixes, security patches, or feature development. Using this library in new projects is strongly discouraged.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Manually cast values to numbers after parsing if numeric operations are required: `const parsed = logfmt.parse('count=123'); const count = parseInt(parsed.count, 10);`","message":"The `logfmt.parse()` method does not automatically convert all numeric strings to numbers. It only converts `true` and `false` strings to booleans. Numbers are parsed as strings to avoid precision loss issues with 32-bit representations.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"To create an independent, configurable instance, use `const customLogfmt = new logfmt();`. This will allow you to modify `customLogfmt` without affecting the main singleton instance.","message":"The `logfmt` module is a singleton when imported via `require('logfmt')`. Any modifications to its properties (e.g., `logfmt.stringify = JSON.stringify`) will affect all parts of your application that use this global instance.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"The library primarily targets CommonJS environments and does not officially support ES Modules (ESM). While bundlers might transpile it, direct ESM `import` statements may behave unexpectedly or require specific configuration.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"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.","cause":"Attempting to call `stringify` or other methods on an undefined `logfmt` object, or if the `logfmt` variable was reassigned.","error":"TypeError: logfmt.stringify is not a function"},{"fix":"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.","cause":"This typically occurs when attempting to `pipe` to a `logfmt.streamParser()` or `logfmt.streamStringify()` instance, but the stream object itself is undefined or not a valid stream.","error":"TypeError: Cannot read properties of undefined (reading 'pipe')"},{"fix":"Use `logfmt.parse(yourLogfmtString)` to correctly parse logfmt strings into JavaScript objects. Reserve `JSON.parse()` for actual JSON strings.","cause":"This error happens when a logfmt string is accidentally passed to `JSON.parse()`. Logfmt is a key-value format, not JSON.","error":"SyntaxError: Unexpected token 'o' at JSON.parse (<anonymous>)"},{"fix":"Add `const logfmt = require('logfmt');` at the top of your file to properly import the module.","cause":"The `logfmt` variable was used without being declared or initialized, usually by omitting the `require` statement.","error":"ReferenceError: logfmt is not defined"}],"ecosystem":"npm"}