npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mercurius-logging

v2.0.0

Published

Log the GraphQL details

Downloads

8,801

Readme

mercurius-logging

ci npm JavaScript Style Guide

This plugin add a Log with all the GraphQL details you need.

The issue

By default, Fastify logs a simple request that shows always the same url:

{
  "level": 30,
  "time": 1660395516356,
  "pid": 83316,
  "hostname": "eomm",
  "name": "gateway",
  "reqId": "req-1",
  "req": {
    "method": "POST",
    "url": "/graphql",
    "hostname": "localhost:60767",
    "remoteAddress": "127.0.0.1",
    "remotePort": 60769
  },
  "msg": "incoming request"
}

This output does not let you know which queries or mutations are being executed, unless you print or inspect the GQL payload.

This plugin adds this log output to your application:

{
  "level": 30,
  "time": 1660395516406,
  "pid": 83316,
  "hostname": "eomm",
  "name": "gateway",
  "reqId": "req-1",
  "graphql": {
    "queries": [
      "myTeam",
      "myTeam"
    ]
  }
}

When the request contains some mutations:

{
  "level": 30,
  "time": 1660395516406,
  "pid": 83316,
  "hostname": "eomm",
  "name": "gateway",
  "reqId": "req-1",
  "graphql": {
    "mutations": [
      "resetCounter"
    ]
  }
}

Here a complete example when you turn on all the log options:

{
  "level": 30,
  "time": 1660395516406,
  "pid": 83316,
  "hostname": "eomm",
  "name": "gateway",
  "reqId": "req-1",
  "graphql": {
    "queries": [
      "a:add",
      "b:add",
      "c:add",
      "d:add"
    ],
    "operationName": "baam",
    "body": "
      query boom($num: Int!) {
        a: add(x: $num, y: $num)
        b: add(x: $num, y: $num)
      }
      query baam($num: Int!, $bin: Int!) {
        c: add(x: $num, y: $bin)
        d: add(x: $num, y: $bin)
    }",
    "variables": {
      "num": 2,
      "bin": 3
    }
  }
}

If the mercurius graphql decorator is used, it is necessary to provide a context object: app.graphql(query, { reply }). Otherwise, this plugin will ignore the request.

Install

npm install mercurius-logging

Compatibility

| Plugin version | Fastify version | | ------------- |:---------------:| | ^2.0.0 | ^5.0.0 | | ^1.0.0 | ^4.0.0 |

Usage

const Fastify = require('fastify')
const mercurius = require('mercurius')
const mercuriusLogging = require('mercurius-logging')

const app = Fastify({
  logger: true,
  disableRequestLogging: true
})

app.register(mercurius, {
  schema: yourSchema,
  resolvers: yourResolvers
})
app.register(mercuriusLogging)

Options

You can customize the output of the plugin by passing an options object:

app.register(mercuriusLogging, {
  logLevel: 'debug', // default: 'info'
  prependAlias: true, // default: false
  logBody: true, // default: false
  logVariables: true, // default: false
  logRequest: true // default: false
  logMessage: function(context) // default: undefined
})

logLevel

The log level of the plugin. Note that the request logger is used, so you will get the additional request data such as the reqId.

logRequest

Add to the log line the req: request object. This is useful if you want to log the request's headers or other. You can customize what to log by using the logSerializers option of Fastify.

const app = Fastify({
  logger: {
    level: 'debug',
    serializers: {
      req: function reqSerializer (req) {
        // look at the standard serializer for the req object:
        // https://github.com/pinojs/pino-std-serializers/
        return {
          headers: req.headers
        }
      }
    }
  }
})

app.register(mercuriusLogging, {
  logRequest: true
})

prependAlias

Queries and mutations may have an alias. If you want to append the alias to the log, set this option to true. You will get the following output:

{
  "level": 30,
  "graphql": {
    "queries": [
      "firstQuery:myTeam",
      "secondQuery:myTeam"
    ]
  }
}

logBody

If you want to include the body of the request in the log output, set this option to true.

You can provide a syncronous function to choose to log the body or not. The function must return true to log the body.

app.register(mercuriusLogging, {
  logBody: function (context, body) {
    return context.reply.request.headers['x-debug'] === 'true'
  }
})

Here an output example:

{
  "level": 30,
  "graphql": {
    "queries": [
      "firstQuery:myTeam",
      "secondQuery:myTeam"
    ],
    "body": "query firstQuery { myTeam { name } } query secondQuery { myTeam { name } }"
  }
}

logVariables

If you want to include the request's variables in the log output, set this option to true.

{
  "level": 30,
  "graphql": {
    "queries": [
      "firstQuery:myTeam",
      "secondQuery:myTeam"
    ],
    "variables": {
      "teamId": 1
    }
  }
}

logMessage

If you want to put a custom message inside the log output, you can set this option as a function(context) which returns a string or an array containing Pino supported values.

Example returning a string

app.register(mercuriusLogging, {
  logMessage: function (context) {
    return `This is a request made with method ${context.reply.request.method}`
  }
})

Here's an output example

{
  "level": 30,
  "graphql": {
    "queries": [
      "firstQuery:myTeam",
      "secondQuery:myTeam"
    ]
  },
  "msg": "This is a request made with method POST"
}

Example returning an array

app.register(mercuriusLogging, {
  logMessage: function (context) {
    return ['This is a request made with method %s by foo%s', context.reply.request.method, 'bar' ]
  }
})

Here's an output example

{
  "level": 30,
  "graphql": {
    "queries": [
      "firstQuery:myTeam",
      "secondQuery:myTeam"
    ]
  },
  "msg": "This is a request made with method POST by foobar"
}

License

Copyright Manuel Spigolon, Licensed under MIT.