{"id":22485120,"url":"https://github.com/Eomm/mercurius-logging","last_synced_at":"2025-08-02T18:31:31.101Z","repository":{"id":57749735,"uuid":"524401290","full_name":"Eomm/mercurius-logging","owner":"Eomm","description":"Log the GraphQL details","archived":false,"fork":false,"pushed_at":"2025-06-01T07:58:04.000Z","size":56,"stargazers_count":17,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-07-19T02:45:44.187Z","etag":null,"topics":["fastify","fastify-plugin","graphql","hacktoberfest","logging","mercurius","plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Eomm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"Eomm"}},"created_at":"2022-08-13T13:03:36.000Z","updated_at":"2025-06-01T07:58:01.000Z","dependencies_parsed_at":"2023-12-18T08:27:50.301Z","dependency_job_id":"c30519ad-892b-4750-8713-0b71f9cad4b9","html_url":"https://github.com/Eomm/mercurius-logging","commit_stats":{"total_commits":40,"total_committers":4,"mean_commits":10.0,"dds":0.525,"last_synced_commit":"1903696a8980674baa24f06f89515d936fbe489d"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/Eomm/mercurius-logging","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fmercurius-logging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fmercurius-logging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fmercurius-logging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fmercurius-logging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eomm","download_url":"https://codeload.github.com/Eomm/mercurius-logging/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Fmercurius-logging/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268432788,"owners_count":24249618,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-plugin","graphql","hacktoberfest","logging","mercurius","plugin"],"created_at":"2024-12-06T17:12:02.520Z","updated_at":"2025-08-02T18:31:31.090Z","avatar_url":"https://github.com/Eomm.png","language":"JavaScript","readme":"# mercurius-logging\n\n[![ci](https://github.com/Eomm/mercurius-logging/actions/workflows/ci.yml/badge.svg)](https://github.com/Eomm/mercurius-logging/actions/workflows/ci.yml)\n[![npm](https://img.shields.io/npm/v/mercurius-logging)](https://www.npmjs.com/package/mercurius-logging)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nThis plugin add a Log with all the GraphQL details you need.\n\n## The issue\n\nBy default, Fastify logs a simple request that shows always the same `url`:\n\n```json\n{\n  \"level\": 30,\n  \"time\": 1660395516356,\n  \"pid\": 83316,\n  \"hostname\": \"eomm\",\n  \"name\": \"gateway\",\n  \"reqId\": \"req-1\",\n  \"req\": {\n    \"method\": \"POST\",\n    \"url\": \"/graphql\",\n    \"hostname\": \"localhost:60767\",\n    \"remoteAddress\": \"127.0.0.1\",\n    \"remotePort\": 60769\n  },\n  \"msg\": \"incoming request\"\n}\n```\n\nThis output does not let you know which queries or mutations are being executed,\nunless you print or inspect the GQL payload.\n\nThis plugin adds this log output to your application:\n\n```json\n{\n  \"level\": 30,\n  \"time\": 1660395516406,\n  \"pid\": 83316,\n  \"hostname\": \"eomm\",\n  \"name\": \"gateway\",\n  \"reqId\": \"req-1\",\n  \"graphql\": {\n    \"queries\": [\n      \"myTeam\",\n      \"myTeam\"\n    ]\n  }\n}\n```\n\nWhen the request contains some mutations:\n\n```json\n{\n  \"level\": 30,\n  \"time\": 1660395516406,\n  \"pid\": 83316,\n  \"hostname\": \"eomm\",\n  \"name\": \"gateway\",\n  \"reqId\": \"req-1\",\n  \"graphql\": {\n    \"mutations\": [\n      \"resetCounter\"\n    ]\n  }\n}\n```\n\nHere a complete example when you turn on all the log options:\n\n```json5\n{\n  \"level\": 30,\n  \"time\": 1660395516406,\n  \"pid\": 83316,\n  \"hostname\": \"eomm\",\n  \"name\": \"gateway\",\n  \"reqId\": \"req-1\",\n  \"graphql\": {\n    \"queries\": [\n      \"a:add\",\n      \"b:add\",\n      \"c:add\",\n      \"d:add\"\n    ],\n    \"operationName\": \"baam\",\n    \"body\": \"\n      query boom($num: Int!) {\n        a: add(x: $num, y: $num)\n        b: add(x: $num, y: $num)\n      }\n      query baam($num: Int!, $bin: Int!) {\n        c: add(x: $num, y: $bin)\n        d: add(x: $num, y: $bin)\n    }\",\n    \"variables\": {\n      \"num\": 2,\n      \"bin\": 3\n    }\n  }\n}\n```\n\nIf the [mercurius `graphql` decorator](https://github.com/mercurius-js/mercurius/blob/master/docs/api/options.md#appgraphqlsource-context-variables-operationname) is used, it is necessary to provide a `context` object: `app.graphql(query, { reply })`.\nOtherwise, this plugin will ignore the request.\n\n## Install\n\n```\nnpm install mercurius-logging\n```\n\n### Compatibility\n\n| Plugin version | Fastify version |\n| ------------- |:---------------:|\n| `^2.0.0` | `^5.0.0` |\n| `^1.0.0` | `^4.0.0` |\n\n## Usage\n\n```js\nconst Fastify = require('fastify')\nconst mercurius = require('mercurius')\nconst mercuriusLogging = require('mercurius-logging')\n\nconst app = Fastify({\n  logger: true,\n  disableRequestLogging: true\n})\n\napp.register(mercurius, {\n  schema: yourSchema,\n  resolvers: yourResolvers\n})\napp.register(mercuriusLogging)\n```\n\n## Options\n\nYou can customize the output of the plugin by passing an options object:\n\n```js\napp.register(mercuriusLogging, {\n  logLevel: 'debug', // default: 'info'\n  prependAlias: true, // default: false\n  logBody: true, // default: false\n  logVariables: true, // default: false\n  logRequest: true // default: false\n  logMessage: function(context) // default: undefined\n})\n```\n\n### logLevel\n\nThe log level of the plugin. Note that the `request` logger is used, so you will get the additional\n[request data](https://www.fastify.io/docs/latest/Reference/Logging/#usage) such as the `reqId`.\n\n### logRequest\n\nAdd to the log line the `req: request` object. This is useful if you want to log the request's headers or other.\nYou can customize what to log by using the `logSerializers` option of Fastify.\n\n```js\nconst app = Fastify({\n  logger: {\n    level: 'debug',\n    serializers: {\n      req: function reqSerializer (req) {\n        // look at the standard serializer for the req object:\n        // https://github.com/pinojs/pino-std-serializers/\n        return {\n          headers: req.headers\n        }\n      }\n    }\n  }\n})\n\napp.register(mercuriusLogging, {\n  logRequest: true\n})\n```\n\n### prependAlias\n\nQueries and mutations may have an alias. If you want to append the alias to the log, set this option to `true`.\nYou will get the following output:\n\n```json\n{\n  \"level\": 30,\n  \"graphql\": {\n    \"queries\": [\n      \"firstQuery:myTeam\",\n      \"secondQuery:myTeam\"\n    ]\n  }\n}\n```\n\n### logBody\n\nIf you want to include the body of the request in the log output, set this option to `true`.\n\nYou can provide a syncronous function to choose to log the body or not.\nThe function must return `true` to log the body.\n\n```js\napp.register(mercuriusLogging, {\n  logBody: function (context, body) {\n    return context.reply.request.headers['x-debug'] === 'true'\n  }\n})\n```\n\nHere an output example:\n\n```json\n{\n  \"level\": 30,\n  \"graphql\": {\n    \"queries\": [\n      \"firstQuery:myTeam\",\n      \"secondQuery:myTeam\"\n    ],\n    \"body\": \"query firstQuery { myTeam { name } } query secondQuery { myTeam { name } }\"\n  }\n}\n```\n\n### logVariables\n\nIf you want to include the request's variables in the log output, set this option to `true`.\n\n```json\n{\n  \"level\": 30,\n  \"graphql\": {\n    \"queries\": [\n      \"firstQuery:myTeam\",\n      \"secondQuery:myTeam\"\n    ],\n    \"variables\": {\n      \"teamId\": 1\n    }\n  }\n}\n```\n\n\n### logMessage\n\nIf 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.\n\n#### Example returning a string\n\n```js\napp.register(mercuriusLogging, {\n  logMessage: function (context) {\n    return `This is a request made with method ${context.reply.request.method}`\n  }\n})\n```\n\nHere's an output example\n\n```json\n{\n  \"level\": 30,\n  \"graphql\": {\n    \"queries\": [\n      \"firstQuery:myTeam\",\n      \"secondQuery:myTeam\"\n    ]\n  },\n  \"msg\": \"This is a request made with method POST\"\n}\n```\n\n#### Example returning an array\n\n```js\napp.register(mercuriusLogging, {\n  logMessage: function (context) {\n    return ['This is a request made with method %s by foo%s', context.reply.request.method, 'bar' ]\n  }\n})\n```\n\nHere's an output example\n\n```json\n{\n  \"level\": 30,\n  \"graphql\": {\n    \"queries\": [\n      \"firstQuery:myTeam\",\n      \"secondQuery:myTeam\"\n    ]\n  },\n  \"msg\": \"This is a request made with method POST by foobar\"\n}\n```\n\n\n\n\n## License\n\nCopyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/Eomm"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEomm%2Fmercurius-logging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEomm%2Fmercurius-logging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEomm%2Fmercurius-logging/lists"}