{"id":21944717,"url":"https://github.com/browserstack/chitragupta-node","last_synced_at":"2025-04-22T21:06:29.542Z","repository":{"id":39994264,"uuid":"341166097","full_name":"browserstack/chitragupta-node","owner":"browserstack","description":"Node SDK for structured JSON logging","archived":false,"fork":false,"pushed_at":"2024-04-24T10:06:43.000Z","size":209,"stargazers_count":2,"open_issues_count":7,"forks_count":3,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-04-22T21:06:23.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/browserstack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-02-22T10:44:43.000Z","updated_at":"2024-02-13T08:07:08.000Z","dependencies_parsed_at":"2024-01-15T15:05:33.914Z","dependency_job_id":null,"html_url":"https://github.com/browserstack/chitragupta-node","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserstack%2Fchitragupta-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserstack%2Fchitragupta-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserstack%2Fchitragupta-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/browserstack%2Fchitragupta-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/browserstack","download_url":"https://codeload.github.com/browserstack/chitragupta-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250324690,"owners_count":21411946,"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":[],"created_at":"2024-11-29T04:16:56.984Z","updated_at":"2025-04-22T21:06:29.504Z","avatar_url":"https://github.com/browserstack.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chitragupta\nThis is a support package for the [winston](https://www.npmjs.com/package/winston) library that adds meta data for the logs and structures them into json.\nThe package is tested with winston 2.x version.\n\n## Installation\n\n```bash\nnpm install https://github.com/browserstack/chitragupta-node\n```\n\n## Usage\n\nThe informative structured logging can be achieved in 2 steps:\n1. Setting up the logger\n2. Setting up the contexts in server/process/worker\n\n### Setting Up the Logger\n\n#### Winston 2.x\n```node\nconst winston = require('winston');\nconst { Chitragupta } = require('chitragupta');\n\n\n// Configure your logger's formatter\nvar logger = new winston.Logger({\n  level: 'info',\n  transports: [\n    new (winston.transports.Console)({\n      formatter: Chitragupta.jsonLogFormatter\n    })]\n});\n```\n\n#### Winston 3.x\n```node\nconst winston = require('winston');\nconst { Chitragupta } = require('chitragupta');\n\nconst jsonLogFormatter = winston.format.printf( ({ level, message, ...meta}) =\u003e {\n  const options = {level: level, message: message, meta: meta};\n  return Chitragupta.jsonLogFormatter(options);\n});\n\nconst logger = winston.createLogger({\n  transports: [\n    new winston.transports.Console({\n      format: jsonLogFormatter,\n    })\n  ],\n});\n```\n\n### Setting up contexts in Server, Services and Background Workers\n\nFor web servers built with node [http](https://nodejs.org/api/http.html) package\n```node\nconst http = require(\"http\");\n\n// Initialize and add meta data for the server\nfunction requestHandler(request, response) {\n  logger.log('info', 'processing request');\n  response.end();\n}\n\nvar server = http.createServer(function(request, response) {\n  // Chitragupta.setupServerLogger(loggerObject, request, response, userId, requestHandler);\n  Chitragupta.setupServerLogger(logger, request, response, 123, requestHandler);\n});\nserver.listen(8080);\n```\nOR\n```node\nfunction requestHandler(request, response) {\n  logger.log('info', 'processing request', {'data': {'request': {'method': 'POST'}}});\n  response.end();\n}\n```\nFor processes/services\n```node\nfunction process_some_crazy_stuff(a, b, c) {\n  logger.log('info', a);\n  logger.log('info', b);\n  logger.log('info', c);\n}\n\n// Chitragupta.setupProcessLogger(uniqueNameOfTheProcess, functionToBeCalled, all, the, args, that, you, would, like, to, pass);\nChitragupta.setupProcessLogger('processing_crazy_stuff', process_some_crazy_stuff, 1, true, 45);\n```\nOR\n```node\nfunction process_some_crazy_stuff(a, b, c) {\n  logger.log('info', a, {'data': {'name': 'process_name', 'execution_id': 'some_unique_id'}});\n  logger.log('info', b, {'data': {'name': 'some_other_process_name', 'execution_id': 'some_other_unique_id'}});\n}\n```\nFor workers\n```node\nconst processFromQueue = (n) =\u003e {\n  getCurrentQueue()\n    .then(someAwesomeStuff)\n    .catch(e =\u003e {\n      logger.log('info', 'Exceptions');\n      setTimeout(getWorkFromRedis, 1000);\n    });\n}\n\nconst getWorkFromRedis = (n, first_run) =\u003e {\n  // Use the following function to set the worker job id if required\n  // Chitragupta.setWorkerJobId('unique-job-id', functionToBeCalled, all, the, args, that, you, would, like, to, pass);\n  Chitragupta.setWorkerJobId(jobId, processFromQueue, n);\n}\n\n// Chitragupta.setupWorkerLogger(uniqueNameOfTheWorker, functionToBeCalled, all, the, args, that, you, would, like, to, pass);\nChitragupta.setupWorkerLogger('redisWorker', getWorkFromRedis, 1, true);\n```\nOR\n```node\nconst processFromQueue = (n) =\u003e {\n  getCurrentQueue()\n    .then(someAwesomeStuff)\n    .catch(e =\u003e {\n      logger.log('info', 'Exceptions', {'data': {'worker_name': 'worker_name', 'thread_id': 'some_thread_id'}});\n      setTimeout(getWorkFromRedis, 1000);\n    });\n}\n```\n### Additional Features\n1. For the logs where `log.kind` is missing, first 40 chars of dynamic_data is populated to uniquely identify each log in a restricted fashion.\n2. For the logs where no contexts are set, ie. server, process or worker are not set, server format category and version is populated.\n3. `request.request_id` and the `request_start_time` can be set manually if required.\n\n## Contributing\n\nRun eslint using the following command\n```bash\n# Please install the eslint supported node version using `nvm install v12.14.1`\nnpx eslint .\n```\nBug reports and pull requests are welcome on GitHub at https://github.com/browserstack/chitragupta-node.\n\n## License\n\nThe package is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserstack%2Fchitragupta-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrowserstack%2Fchitragupta-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrowserstack%2Fchitragupta-node/lists"}