Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jclem/logfmt2
A logfmt encoder and decoder
https://github.com/jclem/logfmt2
logfmt logging
Last synced: 2 months ago
JSON representation
A logfmt encoder and decoder
- Host: GitHub
- URL: https://github.com/jclem/logfmt2
- Owner: jclem
- License: mit
- Created: 2018-09-26T22:59:33.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2023-01-07T03:59:21.000Z (almost 2 years ago)
- Last Synced: 2024-10-05T08:14:39.878Z (3 months ago)
- Topics: logfmt, logging
- Language: TypeScript
- Homepage: https://npm.im/@jclem/logfmt2
- Size: 794 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# logfmt2
[![](https://github.com/jclem/logfmt2/workflows/Test%20%26%20Benchmark/badge.svg)](https://github.com/jclem/logfmt2/actions)
logfmt2 (which is based on the original [logfmt](https://github.com/csquared/node-logfmt) and the original [blog post](https://brandur.org/logfmt)) is a module for encoding objects into the logfmt format and decoding them again.
## Install
```
npm install @jclem/logfmt2
```## Build
```
script/build
```## Publish
The `script/publish` script cleans the build directory, builds the project, and then runs `npm publish`.
```
script/publish
```## Usage
```javascript
const {Logger, encode, decode} = require('@jclem/logfmt2')console.log(encode({foo: 'bar'})) // foo=bar
console.log(decode('foo=bar')) // {foo: 'bar'}// Use the static `Logger.log` to stdout
Logger.log({foo: 'bar'}) // logs "foo=bar"// Create a logger to maintain a logging context
const logger = new Logger({ns: 'my-app'})
logger.log({foo: 'bar'}) // logs "ns=my-app foo=bar"// Add timers
logger.time('elapsedMs')
// Wait 50ms
logger.log({foo: 'bar'}) // logs "ns=my-app elapsedMs=50 foo=bar"
// Wait 50ms
logger.log({foo: 'bar'}) // logs "ns=my-app elapsedMs=100 foo=bar"// Add (mutate) the logger context
logger.appendContext({new_context: 'hello'})
logger.log({foo: 'bar'}) // logs "ns=my-app new_context=hello elapsedMs=100 foo=bar"
```