{"id":13832428,"url":"https://github.com/pablosichert/concurrency-logger","last_synced_at":"2025-04-04T21:11:46.022Z","repository":{"id":57205184,"uuid":"71829329","full_name":"pablosichert/concurrency-logger","owner":"pablosichert","description":"Log HTTP requests/responses separately, visualize their concurrency and report logs/errors in context of a request.","archived":false,"fork":false,"pushed_at":"2018-03-08T20:36:54.000Z","size":452,"stargazers_count":392,"open_issues_count":2,"forks_count":8,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-28T20:10:43.320Z","etag":null,"topics":["concurrency","http","koa","logger","logging","middleware","request"],"latest_commit_sha":null,"homepage":"https://pablosichert.github.io/concurrency-logger/","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/pablosichert.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}},"created_at":"2016-10-24T20:28:11.000Z","updated_at":"2025-03-22T13:24:23.000Z","dependencies_parsed_at":"2022-09-12T22:50:37.937Z","dependency_job_id":null,"html_url":"https://github.com/pablosichert/concurrency-logger","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablosichert%2Fconcurrency-logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablosichert%2Fconcurrency-logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablosichert%2Fconcurrency-logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pablosichert%2Fconcurrency-logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pablosichert","download_url":"https://codeload.github.com/pablosichert/concurrency-logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247249537,"owners_count":20908212,"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":["concurrency","http","koa","logger","logging","middleware","request"],"created_at":"2024-08-04T10:02:03.158Z","updated_at":"2025-04-04T21:11:45.955Z","avatar_url":"https://github.com/pablosichert.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# concurrency-logger\n[![NPM version][npm-image]][npm-url]\n[![Build status][travis-image]][travis-url]\n[![Coverage status][coveralls-image]][coveralls-url]\n[![Dependency status][david-dm-image]][david-dm-url]\n[![Dev dependency status][david-dm-dev-image]][david-dm-dev-url]\n\nHTTP logging middleware especially useful to unwind concurrent operations without losing the request context\n\n\u003ca href=\"https://pablosichert.github.io/concurrency-logger/\"\u003e\n    \u003cp align=\"center\"\u003e\n        \u003cimg src=\"https://cloud.githubusercontent.com/assets/4450694/19836607/c60e0ed2-9ea5-11e6-8696-556eed7ea7c9.gif\" alt=\"HTTP logs in a terminal, visualizing server status codes, response times, debug information and errors for concurrent requests\" /\u003e\n        \u003cbr /\u003e\n        Launch demo in your browser\n    \u003c/p\u003e\n\u003c/a\u003e\n\n## Install\n```\n$ npm install concurrency-logger\n```\n\n## Usage\n\n### With [koa](https://github.com/koajs/koa)\n\n#### Basic usage\n```js\nimport Koa from 'koa';\nimport createLogger from 'concurrency-logger';\n\nconst app = new Koa;\n\n// Logger is stateful as it contains information about concurrent requests\n// Same instance needs to be reused across requests\nconst logger = createLogger(/* options */);\n\napp.use(logger);\n```\n\n#### Log from middleware\n```js\n// Log something in context to a specific request to trace it back easily -\n// also when there are multiple concurrent requests\napp.use(async (context, next) =\u003e {\n    context.log('Log!');\n    context.log.info('Info!');\n    context.log.error('Error!');\n\n    await next();\n});\n```\n\n#### Attach more [context](https://github.com/koajs/koa/blob/master/docs/api/context.md#request-aliases) to the log\n```js\nconst logger = createLogger({\n    req: context =\u003e (\n        context.originalUrl + '\\n' +\n        context.get('User-Agent')\n    )\n});\n```\n\n#### Include localized timestamps\n```js\nconst logger = createLogger({\n    timestamp: true\n});\n```\n\n#### Write log to file\n```js\nimport { createWriteStream } from 'fs';\n\n// To read log use program that interprets ANSI escape codes,\n// e.g. cat or less -r\nconst log = createWriteStream('logs/requests.log');\n\nconst logger = createLogger({\n    reporter: log\n});\n```\n\n#### Adjust alert levels per method and response time\n```js\nconst logger = createLogger({\n    getLevel: (responseTime, context) =\u003e {\n        /*\n            GET\n              0 -  99ms: 0\n            100 - 149ms: 1\n            150 - 199ms: 2\n            200 - 249ms: 3\n            250 - 299ms: 4\n            300 - 349ms: 5\n            \u003e 350ms    : 6\n\n            POST\n              0 - 149ms: 0\n            150 - 225ms: 1\n                   ... : ...\n        */\n\n        let threshold = 50; // ms\n\n        if (['POST', 'PUT'].includes(context.method)) {\n            threshold *= 1.5;\n        }\n\n        return Math.floor(responseTime / threshold) - 1;\n    }\n});\n```\n\n### Standalone\n```js\nimport createLogger from 'concurrency-logger';\n\nconst logger = createLogger(/* options */);\n\n(async () =\u003e {\n    const context = {\n        method: 'GET',\n        originalUrl: '/'\n    };\n\n    const next = async () =\u003e {\n        await new Promise(resolve =\u003e setTimeout(resolve, 100));\n\n        context.status = 200;\n    };\n\n    try {\n        await logger(context, next);\n    } catch (error) {\n        // Errors are passed through\n    }\n})();\n```\n\n## API\n| Option | Type | Default | Description | Example |\n| ---- | ---- | ------- | ----------- | ------- |\n| minSlots | integer | `1` | Amount of space that is provisioned to display concurrent request lanes. Number of lanes will automatically scale up as the number of concurrent requests grow. | `3`\n| getLevel | integer: function(responseTime: integer) | `responseTime =\u003e Math.floor(responseTime / 50) - 1` | Map response time to alert level. Alert levels go from 0 (default color) to 6 (dark red). By default that means `\u003c100ms: 0`, `\u003c150ms: 1` `\u003c200ms: 2`, ..., `\u003e=350ms: 6`. | `responseTime =\u003e Math.floor(responseTime / 100)`\n| width | integer, boolean(`false`) | `undefined` | If no width is provided, it will be dynamically read from `process.stdout.columns`. Pass in an integer to break all lines according to the specified fixed (terminal character) width. Pass in `false` if you want the lines not to break at all. | `80`, `132`, `false`\n| timestamp | boolean | `false` | Print localized timestamp for every requests. | `true`, `false`\n| slim | boolean | `false` | \"Slim mode\": don't use an extra character between request lanes to shrink width, but make them harder to separate visually. | `true`, `false`\n| reporter | writable stream | `process.stdout` | Specify a stream that handles the output lines. Write to terminal or stream to a log file, for example. Note that the lines contain ANSI color codes, so when streaming to a file you might need a program that can read those. E.g. `less -r requests.log` | `require('fs').createWriteStream('logs/requests.log')`\n| req | any: function(context: object) | `context =\u003e context.originalUrl` | Attach additional information to the request log line. | `context =\u003e context.originalUrl + '\\n' + context.get('User-Agent')`\n| res | any: function(context: object) | `context =\u003e context.originalUrl` | Attach additional information to the response log line. | `context =\u003e context.originalUrl + '\\n' + context.get('User-Agent')`\n\n## Developing\nInstall development dependencies\n```\n$ npm install\n```\n\nCreate new fixtures to test against\n```\n$ npm run create-fixtures\n```\n\nManually review fixtures (you need a program that renders ANSI escape codes)\n```\n$ less -r test/fixtures/*\n```\n\nRun tests\n```\n$ npm test\n```\n\nRun code linter\n```\n$ npm run lint\n```\n\nCompile to ES5 from /src to /lib\n```\n$ npm run compile\n```\n\nInitialize demo project\n```\n$ git clone git@github.com:PabloSichert/concurrency-logger demo\n$ cd demo\ndemo $ git checkout gh-pages\ndemo $ npm install\n```\n\nBuild demo\n```\ndemo $ npm run compile\n```\n\n[npm-url]: https://npmjs.org/package/concurrency-logger\n[npm-image]: https://badge.fury.io/js/concurrency-logger.svg\n[travis-url]: https://travis-ci.org/PabloSichert/concurrency-logger?branch=master\n[travis-image]: https://travis-ci.org/PabloSichert/concurrency-logger.svg?branch=master\n[coveralls-url]:https://coveralls.io/r/PabloSichert/concurrency-logger?branch=master\n[coveralls-image]:https://coveralls.io/repos/PabloSichert/concurrency-logger/badge.svg?branch=master\n[david-dm-url]:https://david-dm.org/PabloSichert/concurrency-logger\n[david-dm-image]:https://david-dm.org/PabloSichert/concurrency-logger.svg\n[david-dm-dev-url]:https://david-dm.org/PabloSichert/concurrency-logger#info=devDependencies\n[david-dm-dev-image]:https://david-dm.org/PabloSichert/concurrency-logger/dev-status.svg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpablosichert%2Fconcurrency-logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpablosichert%2Fconcurrency-logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpablosichert%2Fconcurrency-logger/lists"}