{"id":15685606,"url":"https://github.com/eomm/fastify-log-controller","last_synced_at":"2025-05-07T17:21:24.747Z","repository":{"id":154228920,"uuid":"630328169","full_name":"Eomm/fastify-log-controller","owner":"Eomm","description":"Control your application logs from your dashboard, at runtime","archived":false,"fork":false,"pushed_at":"2024-11-25T01:02:26.000Z","size":22,"stargazers_count":9,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-01T13:02:43.126Z","etag":null,"topics":["fastify","fastify-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},"funding":{"github":"Eomm"}},"created_at":"2023-04-20T06:36:36.000Z","updated_at":"2024-11-17T10:34:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"59d8b97b-2325-40ed-8b01-79b62e202904","html_url":"https://github.com/Eomm/fastify-log-controller","commit_stats":{"total_commits":8,"total_committers":2,"mean_commits":4.0,"dds":0.125,"last_synced_commit":"910d41745b7174a463b4aa487a85d8aa8934104e"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-log-controller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-log-controller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-log-controller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Eomm%2Ffastify-log-controller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Eomm","download_url":"https://codeload.github.com/Eomm/fastify-log-controller/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252922307,"owners_count":21825641,"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","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"],"created_at":"2024-10-03T17:27:29.445Z","updated_at":"2025-05-07T17:21:24.697Z","avatar_url":"https://github.com/Eomm.png","language":"JavaScript","funding_links":["https://github.com/sponsors/Eomm"],"categories":[],"sub_categories":[],"readme":"# fastify-log-controller\n\n[![ci](https://github.com/Eomm/fastify-log-controller/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Eomm/fastify-log-controller/actions/workflows/ci.yml)\n[![npm](https://img.shields.io/npm/v/fastify-log-controller)](https://www.npmjs.com/package/fastify-log-controller)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\nControl your application logs from your dashboard, at runtime!\n\n\n## Install\n\n```\nnpm i fastify-log-controller\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\n## Usage\n\nWhen you register this plugin, a new route will be available at `/log-level` (by default)\nthat allows you to change the log level of your application at runtime!\n\nYou will be able to change the log level for every tracked encapsulated context!\n\nThis is useful when you want to change the log level of your application without restarting it and\nresetting what is in memory.\n\nLet's see an example:\n\n```js\nasync function example () {\n  const app = require('fastify')({\n    logger: {\n      level: 'error'\n    }\n  })\n\n  // Register the plugin\n  app.register(require('fastify-log-controller'))\n\n  const routes = async function plugin (app, opts) {\n    app.get('/', async function handler (request, reply) {\n      request.log.info('hello world')\n      return { hello: 'world' }\n    })\n  }\n\n  // Create an encapsulated context with register and set the `logCtrl` option\n  app.register(routes, {\n    logCtrl: { name: 'bar' }\n  })\n\n  // Check that the route doesn't log anything because the application log level is `error`\n  await app.inject('/')\n\n  // Change the log level of the `bar` context to `debug`\n  await app.inject({\n    method: 'POST',\n    url: '/log-level',\n    body: {\n      level: 'debug',\n      contextName: 'bar'\n    }\n  })\n\n  // Check that the route logs the `hello world` message!\n  await app.inject('/')\n}\n\nexample()\n```\n\nNote that it works with [custom logger levels](https://github.com/pinojs/pino/blob/master/docs/api.md#customlevels-object) too!\n\nIf the `exposeGet` configuration option is set to `true`, the same route allows you to recover available contexts and currently set log levels:\n\n```sh\n$ curl http://localhost:3000/log-level\n[{\"contextName\":\"bar\",\"level\":\"debug\"}]\n```\n\nand the '/log-level/levels' route allows you to recover available log levels:\n\n```sh\n$ curl http://localhost:3000/log-level/levels\n[\"trace\",\"debug\",\"info\",\"warn\",\"error\",\"fatal\"]\n```\n\nIf you want to go deeper into the encapsulated context concept, you can read these sources:\n\n- [YouTube Video](https://www.youtube.com/watch?v=BnnL7fAKqNU)\n- [Complete Guide to Fastify plugin system](https://backend.cafe/the-complete-guide-to-the-fastify-plugin-system)\n- [What is the exact use of `fastify-plugin`](https://stackoverflow.com/questions/61020394/what-is-the-exact-use-of-fastify-plugin/61054534#61054534)\n\n\n### Known limitations\n\nIn fastify you can **voluntarily** set log level for every encapsulated context and route, but you can't change it at runtime.  \nIn these cases, the log level of the encapsulated context will be **preserved**.\n\n#### Plugin log level\n\n```js\napp.register(async function plugin (app) {\n  // The log level will be always `fatal`\n}, {\n  logLevel: 'fatal',\n  logCtrl: { name: 'bar' }\n})\n```\n\n#### Route log level\n\n```js\napp.register(async function plugin (app) {\n  app.get('/bar', {\n    logLevel: 'debug',\n    handler: (request, reply) =\u003e {\n      // Here the log level will be always `debug`\n      return {}\n    }\n  })\n  \n  app.get('/bar', {\n    handler: (request, reply) =\u003e {\n      // Here the log level can be changed at runtime!\n      return {}\n    }\n  })\n}, { logCtrl: { name: 'foo' } })\n```\n\n\n## Options\n\nYou can pass some options to the plugin:\n\n```js\napp.register(require('fastify-log-controller'), {\n  // How you want to call the option in the encapsulated context\n  optionKey: 'logCtrl',\n\n  // Enable get routes\n  exposeGet: false,\n\n  // Enhance the route config of the log controller route\n  // It is not possible to change the handler and the schema\n  routeConfig: {\n    // Any option accepted by fastify route:\n    // https://www.fastify.io/docs/latest/Reference/Routes/#routes-options\n  }\n})\n```\n\nRemember that you must pass the same `optionKey` to the encapsulated context:\n\n```js\napp.register(routes, {\n  logCtrl: { name: 'bar' }\n})\n```\n\n\n## License\n\nCopyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Ffastify-log-controller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feomm%2Ffastify-log-controller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feomm%2Ffastify-log-controller/lists"}