Coder Social home page Coder Social logo

dnjstrom / pino-dev Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 1.0 6.77 MB

A simple prettifier for pino with built-in support for commonly used ecosystem packages.

License: MIT License

JavaScript 9.24% TypeScript 89.46% Shell 0.95% Dockerfile 0.35%
pino pino-pretty pino-http pino-debug ndjson prettifier logging

pino-dev's Introduction

pino-dev

A simple prettifier for pino with built-in support for common ecosystem packages. Designed to be clear, unintrusive and to the point with sensible defaults optimized for development use.

Node.js CI Codeql Analysis

Screenshot

Supported packages

Is your favorite pino package not yet supported? Request support here.

Issues

Discovered a problem with pino-dev? Report an issue.

Quickstart

Install using one of the commands below

npm install --save-dev pino-dev
yarn add --dev pino-dev

then pipe the output of your pino-enhanced app to the pino-dev cli in your package.json scripts.

{
  "scripts": {
    "dev": "./start-your-app | pino-dev"
  }
}

In some situations using npx can also be convenient to pipe arbitrary commands through pino-dev:

cat server.log | npx pino-dev

Configuration

Configuration can be stored as a pino-dev.config.json or pino-dev.config.js file at the root of your repo. It's also possible to use a "pino-dev": {...} key in your package.json.

Default config

{
  "newline": "\n",
  "timeFormat": "HH:mm:ss",
  "colorize": undefined,
  "propertyMap": {
    "msg": "msg",
    "level": "level",
    "ns": "ns",
    "name": "name",
    "err.stack": "err.stack",
    "time": "time",
    "req.method": "req.method",
    "req.url": "req.url",
    "res.statusCode": "res.statusCode",
    "responseTime": "responseTime"
  }
}

newline (string)

The newline character used in prettified output. Usually dependent on your operating system, it's either "\n" (default) or "\r\n".

time-format (string)

The time format to use (syntax according to https://www.npmjs.com/package/date-fns).

colorize (boolean)

Forces colors on or off. If left undefined color support is detected automatically.

propertyMap (Record<string, string>)

This configuration allows you to map arbitrary incoming properties to semantic pino-dev properties using json. For instance,

echo '{"message": "foobar"}' | pino-dev --property-map '{"msg": "message"}'

would map the message property in the incoming json to the semantic property msg which enables pino-dev to understand how to format the log. For deep properties it's possible to use dot-notation, and mapping to a boolean false will disable the default mapping (e.g. pino-dev --property-map '{"name": false}' won't display the name in the prettified output).

Command line arguments

It's also possible to specify/override configuration with command-line arguments:

Usage: pino-dev [options]

    --, --color         Force color.
    -h, --help          Output usage information
    -n, --newline       The newline character used in prettified output. Either "\n" (default) or "\r\n".
    --, --no-color      Force no color.
    -m, --property-map  Map arbitrary incoming properties to semantic pino-dev properties using json.
    -t, --time-format   The time format to use (syntax according to https://www.npmjs.com/package/date-fns).
    -v, --version       Output the version number

Programmatic usage

Pino v7+ transports are supported and can be used as follows:

import pino from "pino";
const transport = pino.transport({
  target: "pino-dev",
});
const logger = pino(transport);

pino-dev's People

Contributors

dependabot[bot] avatar dnjstrom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

epegzz

pino-dev's Issues

v2.0.7 pino-dev: command not found

installed by yarn v1.22.15

  "scripts": {
    "dev": "ts-node-dev --no-notify --respawn --transpile-only src/index.ts -vvv | npx pino-dev"
  },
sh: pino-dev: command not found

i can install it with v2.0.6, but got date-fns error:

Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings. See: https://git.io/fjule

Most CLI options not working

I initially discovered that passing --time-format on the CLI has no effect, and further exploration has shown that there's kind of a mess going on all around.

Things I've found turning on debug output:

  • --newline, --time-format, and --property-map are not applied to resolved configuration, so they don't actually do anything
  • The short form -n is interpreted as --noColor instead of --newline, though using --newline correctly populates the short alias -n
  • --color and --no-color seem to work
  • Configuration via config file works fine, and the debug-printed config matches it + defaults

My suspicion is that the arguments and file config are being merged backwards, i.e. command line args are being overwritten by the already-combined config file + defaults map, so any arguments are just obliterated. That would explain why the color options still work, since they don't appear in the default config.

With long form arguments:

Note the correct options are logged, but the resolved config is just the default.

node src/index.js | DEBUG=* pino-dev --time-format HH:mm:ss --newline "\r\n" --property-map '{ "foo": "msg" }'
  pino-dev pino-dev started with options: {
  pino-dev   "t": "HH:mm:ss",
  pino-dev   "timeFormat": "HH:mm:ss",
  pino-dev   "n": "\\r\\n",
  pino-dev   "newline": "\\r\\n",
  pino-dev   "m": "{ \"foo\": \"msg\" }",
  pino-dev   "propertyMap": "{ \"foo\": \"msg\" }"
  pino-dev } +0ms
  pino-dev Using config {
  pino-dev   "newline": "\n",
  pino-dev   "timeFormat": "HH:mm:ss.SSS",
  pino-dev   "propertyMap": {
  pino-dev     "msg": "msg",
  pino-dev     "level": "level",
  pino-dev     "ns": "ns",
  pino-dev     "name": "name",
  pino-dev     "stack": "stack",
  pino-dev     "time": "time",
  pino-dev     "req.method": "req.method",
  pino-dev     "req.url": "req.url",
  pino-dev     "res.statusCode": "res.statusCode",
  pino-dev     "responseTime": "responseTime"
  pino-dev   }
  pino-dev }. +2ms

With short form arguments

Note that the -n is being picked up as noColor instead of newline. I'm guessing the args package implicitly creates short aliases by default, so since no-color is defined first it "steals" the result of parsing -n.

$ node src/index.js | DEBUG=* pino-dev -t HH:mm:ss -n "\r\n" -m '{ "foo": "msg" }'
  pino-dev pino-dev started with options: {
  pino-dev   "t": "HH:mm:ss",
  pino-dev   "timeFormat": "HH:mm:ss",
  pino-dev   "n": "\\r\\n",
  pino-dev   "noColor": "\\r\\n",
  pino-dev   "m": "{ \"foo\": \"msg\" }",
  pino-dev   "propertyMap": "{ \"foo\": \"msg\" }"
  pino-dev } +0ms
  pino-dev Using config {
  pino-dev   "newline": "\n",
  pino-dev   "timeFormat": "HH:mm:ss.SSS",
  pino-dev   "propertyMap": {
  pino-dev     "msg": "msg",
  pino-dev     "level": "level",
  pino-dev     "ns": "ns",
  pino-dev     "name": "name",
  pino-dev     "stack": "stack",
  pino-dev     "time": "time",
  pino-dev     "req.method": "req.method",
  pino-dev     "req.url": "req.url",
  pino-dev     "res.statusCode": "res.statusCode",
  pino-dev     "responseTime": "responseTime"
  pino-dev   }
  pino-dev }. +2ms

Support nestjs-pino

Hey there!

Could you please add support for nestjs-pino?

It works nicely out of the box. I use it with -m '{"ns":"context"}' to print the context, but it looks weird on http requests that have no context:

image

Maybe add a default [HTTP] context (in another color) for http requests?


Edit: logs have a property req.id that allows correlating logs to requests, it might be interesting to display it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.