{"id":16244595,"url":"https://github.com/indexzero/morgan-json","last_synced_at":"2025-03-19T18:33:13.412Z","repository":{"id":12299595,"uuid":"70855332","full_name":"indexzero/morgan-json","owner":"indexzero","description":"A variant of `morgan.compile` that provides format functions that output JSON","archived":false,"fork":false,"pushed_at":"2022-09-26T08:06:27.000Z","size":9,"stargazers_count":12,"open_issues_count":7,"forks_count":7,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-09T08:43:23.093Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/indexzero.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-13T23:09:05.000Z","updated_at":"2024-07-03T09:11:11.000Z","dependencies_parsed_at":"2022-07-21T13:18:24.671Z","dependency_job_id":null,"html_url":"https://github.com/indexzero/morgan-json","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indexzero%2Fmorgan-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indexzero%2Fmorgan-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indexzero%2Fmorgan-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indexzero%2Fmorgan-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/indexzero","download_url":"https://codeload.github.com/indexzero/morgan-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244013980,"owners_count":20383712,"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-10-10T14:19:51.107Z","updated_at":"2025-03-19T18:33:13.167Z","avatar_url":"https://github.com/indexzero.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# morgan-json\n\nA variant of `morgan.compile` that provides format functions that output JSON\n\n## Usage\n\n``` js\nconst json = require('morgan-json');\n// json(string, opts);\n// json(object, opts);\n```\n\nTo put that into a real world example:\n\n``` js\nconst morgan = require('morgan');\nconst express = require('express');\nconst json = require('morgan-json');\n\nconst app = express()\nconst format = json({\n  short: ':method :url :status',\n  length: ':res[content-length]',\n  'response-time': ':response-time ms'\n});\n\napp.use(morgan(format));\napp.get('/', function (req, res) {\n  res.send('hello, world!')\n});\n```\n\nWhen requests to this `express` application come in `morgan` will output a JSON object that looks\nlike:\n\n```\n{\"short\":\"GET / 200\",\"length\":200,\"response-time\":\"2 ms\"}\n```\n\n### Format objects\n\nWhen provided with an object, `morgan-json` returns a function that will output JSON with keys\nfor each of the keys in that object. The value for each key will be the result of evaluating each\nformat string in the object provided. For example:\n\n``` js\nconst morgan = require('morgan');\nconst json = require('morgan-json');\n\nconst format = json({\n  short: ':method :url :status',\n  length: ':res[content-length]',\n  'response-time': ':response-time ms'\n});\n\napp.use(morgan(format));\n```\n\nWill output a JSON object that has keys `short`, `length` and `response-time`:\n\n```\n{\"short\":\"GET / 200\",\"length\":200,\"response-time\":\"2 ms\"}\n```\n\n### Format strings\n\nWhen provided with a format string, `morgan-json` returns a function that outputs JSON with keys\nfor each of the named tokens within the string provided. Any characters trailing after a token\nwill be included in the value for that key in JSON. For example:\n\n``` js\nconst morgan = require('morgan');\nconst json = require('morgan-json');\n\nconst format = json(':method :url :status :res[content-length] bytes :response-time ms');\n\napp.use(morgan(format));\n```\n\nWill output a JSON object that has keys `method`, `url`, `status`, `res` and `response-time`:\n\n```\n{\"method\":\"GET\",\"url\":\"/\",\"status\":\"200\",\"res\":\"10 bytes\",\"response-time\":\"2 ms\"}\n```\n\n### Returning strings vs. Objects\n\nBy default functions returned by `morgan-json` will return strings from `JSON.stringify`. In some\ncases you may want object literals (e.g. if you perform stringification in another layer of your logger). In this case just provide `{ stringify: false }`:\n\n``` js\n``` js\nconst morgan = require('morgan');\nconst winston = require('winston');\nconst json = require('morgan-json');\n\nconst format = json(':method :url :status', { stringify: false });\n\napp.use(morgan(format, {\n  stream: {\n    write: function (obj) {\n      winston.info(obj);\n    }\n  }\n}));\n```\n\nWill output a JSON object that has keys\n```\n\n## Tests\n\n```\nnpm test\n```\n\n##### LICENSE: MIT\n##### AUTHOR: [Charlie Robbins](https://github.com/indexzero)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findexzero%2Fmorgan-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Findexzero%2Fmorgan-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findexzero%2Fmorgan-json/lists"}