{"_id":"morgan@1.12.1","bugs":{"url":"https://github.com/expressjs/morgan/issues"},"dist":{"shasum":"a6b29a9db7edf55256c560a1ae614c1c03619217","tarball":"http://registry-npm.ywwl.com/morgan/download/morgan-1.12.1.tgz","integrity":"sha512-tljKC0ex20AjO58Ob/eZ53JloycbVswbVNCHx6V6VLGzqt/w8dIynVGL0G8qVjNKwiA7sYSogCrN6QtJ82IV+g==","fileCount":4,"unpackedSize":27776,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/morgan@1.12.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCgWVqEG8Zc4eLm0wht27jAt0YmBIYsG6KAXRW3p6PPxQIhAPnDIAn/fl+7I1lc0emlrbEzj3uTPR5pRRGsG/5giVN5"}],"size":9087,"noattachment":false,"key":"/morgan/-/morgan-1.12.1.tgz"},"name":"morgan","engines":{"node":">= 0.8.0"},"funding":{"url":"https://opencollective.com/express","type":"opencollective"},"gitHead":"b1272e70812dbc504321eeb5a43d14b0822c150d","license":"MIT","scripts":{"lint":"eslint --plugin markdown --ext js,md .","test":"mocha --check-leaks --reporter spec test/morgan.js && mocha --check-leaks --reporter spec test/noColor.js","test-ci":"nyc --reporter=lcov --reporter=text npm test","test-cov":"nyc --reporter=html --reporter=text npm test"},"version":"1.12.1","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:1bb2d837-b117-4d3e-a908-932d23f8436d"},"approver":{"name":"ulisesgascon","email":"ulisesgascondev@gmail.com"}},"homepage":"https://github.com/expressjs/morgan#readme","keywords":["express","http","logger","middleware"],"repository":{"url":"git+https://github.com/expressjs/morgan.git","type":"git"},"_npmVersion":"12.0.2","description":"HTTP request logger middleware for node.js","directories":{},"maintainers":[{"name":"dougwilson","email":"doug@somethingdoug.com"},{"name":"ulisesgascon","email":"ulisesgascondev@gmail.com"},{"name":"jonchurch","email":"npm@jonchurch.com"}],"_nodeVersion":"24.20.0","contributors":[{"name":"Douglas Christopher Wilson","email":"doug@somethingdoug.com"},{"url":"http://jongleberry.com","name":"Jonathan Ong","email":"me@jongleberry.com"}],"dependencies":{"depd":"~2.0.0","debug":"2.6.9","basic-auth":"~2.0.1","on-headers":"~1.1.0","on-finished":"~2.4.1"},"devDependencies":{"nyc":"15.1.0","mocha":"10.8.2","split":"1.0.1","eslint":"6.8.0","supertest":"4.0.2","eslint-plugin-node":"11.1.0","eslint-plugin-import":"2.32.0","eslint-plugin-promise":"4.3.1","eslint-config-standard":"14.1.1","eslint-plugin-markdown":"1.0.2","eslint-plugin-standard":"4.1.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/morgan_1.12.1_1789117062291_0.785786054539984"},"_hasShrinkwrap":false,"readme":"# morgan\n\n[![NPM Version][npm-version-image]][npm-url]\n[![NPM Downloads][npm-downloads-image]][npm-url]\n[![Build Status][ci-image]][ci-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n\nHTTP request logger middleware for node.js\n\n> Named after [Dexter](https://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.\n\n## Installation\n\nThis is a [Node.js](https://nodejs.org/en/) module available through the\n[npm registry](https://www.npmjs.com/). Installation is done using the\n[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):\n\n```sh\n$ npm install morgan\n```\n\n## API\n\nFor ES Modules:\n\n<!-- eslint-disable no-unused-vars -->\n\n```js\nimport morgan from 'morgan'\n```\n\nFor CommonJS:\n\n<!-- eslint-disable no-unused-vars -->\n\n```js\nvar morgan = require('morgan')\n```\n\n### morgan(format, options)\n\nCreate a new morgan logger middleware function using the given `format` and `options`.\nThe `format` argument may be a string of a predefined name (see below for the names),\na string of a format string, or a function that will produce a log entry.\n\nThe `format` function will be called with three arguments `tokens`, `req`, and `res`,\nwhere `tokens` is an object with all defined tokens, `req` is the HTTP request and `res`\nis the HTTP response. The function is expected to return a string that will be the log\nline, or `undefined` / `null` to skip logging.\n\n#### Using a predefined format string\n\n<!-- eslint-disable no-undef -->\n\n```js\nmorgan('tiny')\n```\n\n#### Using format string of predefined tokens\n\n<!-- eslint-disable no-undef -->\n\n```js\nmorgan(':method :url :status :res[content-length] - :response-time ms')\n```\n\n#### Using a custom format function\n\n<!-- eslint-disable no-undef -->\n\n``` js\nmorgan(function (tokens, req, res) {\n  return [\n    tokens.method(req, res),\n    tokens.url(req, res),\n    tokens.status(req, res),\n    tokens.res(req, res, 'content-length'), '-',\n    tokens['response-time'](req, res), 'ms'\n  ].join(' ')\n})\n```\n\n#### Options\n\nMorgan accepts these properties in the options object.\n\n##### immediate\n\nWrite log line on request instead of response. This means that requests will\nbe logged even if the server crashes, _but data from the response (like the\nresponse code, content length, etc.) cannot be logged_.\n\n##### skip\n\nFunction to determine if logging is skipped, defaults to `false`. This function\nwill be called as `skip(req, res)`.\n\n<!-- eslint-disable no-undef -->\n\n```js\n// EXAMPLE: only log error responses\nmorgan('combined', {\n  skip: function (req, res) { return res.statusCode < 400 }\n})\n```\n\n##### stream\n\nOutput stream for writing log lines, defaults to `process.stdout`.\n\nIf the stream is in object mode (`stream.writableObjectMode` is `true`) and the\nformat function returns an object, the object is written to the stream as-is,\nwithout a trailing newline. This allows passing structured log entries to\nloggers that accept objects:\n\n<!-- eslint-disable no-undef -->\n\n```js\nmorgan(function (tokens, req, res) {\n  return {\n    method: tokens.method(req, res),\n    url: tokens.url(req, res),\n    status: Number(tokens.status(req, res))\n  }\n}, {\n  stream: {\n    writableObjectMode: true,\n    write: function (entry) {\n      // entry is the object returned by the format function\n    }\n  }\n})\n```\n\n#### Predefined Formats\n\nThere are various pre-defined formats provided:\n\n##### combined\n\nStandard Apache combined log output.\n```\n:remote-addr - :remote-user [:date[clf]] \":method :url HTTP/:http-version\" :status :res[content-length] \":referrer\" \":user-agent\"\n# will output\n::1 - - [27/Nov/2024:06:21:42 +0000] \"GET /combined HTTP/1.1\" 200 2 \"-\" \"curl/8.7.1\"\n```\n\n##### common\n\nStandard Apache common log output.\n\n```\n:remote-addr - :remote-user [:date[clf]] \":method :url HTTP/:http-version\" :status :res[content-length]\n# will output\n::1 - - [27/Nov/2024:06:21:46 +0000] \"GET /common HTTP/1.1\" 200 2\n```\n\n##### dev\n\nConcise output colored by response status for development use. The `:status`\ntoken will be colored green for success codes, red for server error codes,\nyellow for client error codes, cyan for redirection codes, and uncolored\nfor information codes.\n\n```\n:method :url :status :response-time ms - :res[content-length]\n# will output\nGET /dev 200 0.224 ms - 2\n```\n\nColoring is disabled when the [`NO_COLOR`](https://no-color.org) environment\nvariable is set to any non-empty value, in which case the output above is\nwritten without any escape sequences. The variable is read once, when `morgan`\nis first required.\n\n##### short\n\nShorter than default, also including response time.\n\n```\n:remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms\n# will output\n::1 - GET /short HTTP/1.1 200 2 - 0.283 ms\n```\n\n##### tiny\n\nThe minimal output.\n\n```\n:method :url :status :res[content-length] - :response-time ms\n# will output\nGET /tiny 200 2 - 0.188 ms\n```\n\n#### Tokens\n\n##### Creating new tokens\n\nTo define a token, simply invoke `morgan.token()` with the name and a callback function.\nThis callback function is expected to return a string value. The value returned is then\navailable as \":type\" in this case:\n\n<!-- eslint-disable no-undef -->\n\n```js\nmorgan.token('type', function (req, res) { return req.headers['content-type'] })\n```\n\nCalling `morgan.token()` using the same name as an existing token will overwrite that\ntoken definition.\n\nThe token function is expected to be called with the arguments `req` and `res`, representing\nthe HTTP request and HTTP response. Additionally, the token can accept further arguments of\nits choosing to customize behavior.\n\n##### :date[format]\n\nThe current date and time in UTC. The available formats are:\n\n  - `clf` for the common log format (`\"10/Oct/2000:13:55:36 +0000\"`)\n  - `iso` for the common ISO 8601 date time format (`2000-10-10T13:55:36.000Z`)\n  - `web` for the common RFC 1123 date time format (`Tue, 10 Oct 2000 13:55:36 GMT`)\n\nIf no format is given, then the default is `web`.\n\n##### :http-version\n\nThe HTTP version of the request.\n\n##### :method\n\nThe HTTP method of the request.\n\n##### :pid\n\nThe process ID of the Node.js process handling the request.\n\n##### :referrer\n\nThe Referrer header of the request. This will use the standard mis-spelled Referer header if it exists, otherwise Referrer.\n\n##### :remote-addr\n\nThe remote address of the request. This will use `req.ip`, otherwise the standard `req.connection.remoteAddress` value (socket address).\n\n##### :remote-user\n\nThe user authenticated as part of Basic auth for the request.\n\n##### :req[header]\n\nThe given `header` of the request. If the header is not present, the\nvalue will be displayed as `\"-\"` in the log.\n\n##### :res[header]\n\nThe given `header` of the response. If the header is not present, the\nvalue will be displayed as `\"-\"` in the log.\n\n##### :response-time[digits]\n\nThe time between the request coming into `morgan` and when the response\nheaders are written, in milliseconds.\n\nThe `digits` argument is a number that specifies the number of digits to\ninclude on the number, defaulting to `3`, which provides microsecond precision.\n\n##### :status\n\nThe status code of the response.\n\nIf the request/response cycle completes before a response was sent to the\nclient (for example, the TCP socket closed prematurely by a client aborting\nthe request), then the status will be empty (displayed as `\"-\"` in the log).\n\n##### :total-time[digits]\n\nThe time between the request coming into `morgan` and when the response\nhas finished being written out to the connection, in milliseconds.\n\nThe `digits` argument is a number that specifies the number of digits to\ninclude on the number, defaulting to `3`, which provides microsecond precision.\n\n##### :url\n\nThe URL of the request. This will use `req.originalUrl` if it exists, otherwise `req.url`.\n\n##### :user-agent\n\nThe contents of the User-Agent header of the request.\n\n### morgan.compile(format)\n\nCompile a format string into a `format` function for use by `morgan`. A format string\nis a string that represents a single log line and can utilize token syntax.\nTokens are referenced by `:token-name`. If tokens accept arguments, they can\nbe passed using `[]`, for example: `:token-name[pretty]` would pass the string\n`'pretty'` as an argument to the token `token-name`.\n\nThe function returned from `morgan.compile` takes three arguments `tokens`, `req`, and\n`res`, where `tokens` is an object with all defined tokens, `req` is the HTTP request and\n`res` is the HTTP response. The function will return a string that will be the log line,\nor `undefined` / `null` to skip logging.\n\nNormally formats are defined using `morgan.format(name, format)`, but for certain\nadvanced uses, this compile function is directly available.\n\n## Examples\n\n### express/connect\n\nSample app that will log all requests in the Apache combined format to STDOUT\n\n```js\nvar express = require('express')\nvar morgan = require('morgan')\n\nvar app = express()\n\napp.use(morgan('combined'))\n\napp.get('/', function (req, res) {\n  res.send('hello, world!')\n})\n```\n\n### vanilla http server\n\nSample app that will log all requests in the Apache combined format to STDOUT\n\n```js\nvar finalhandler = require('finalhandler')\nvar http = require('http')\nvar morgan = require('morgan')\n\n// create \"middleware\"\nvar logger = morgan('combined')\n\nhttp.createServer(function (req, res) {\n  var done = finalhandler(req, res)\n  logger(req, res, function (err) {\n    if (err) return done(err)\n\n    // respond to request\n    res.setHeader('content-type', 'text/plain')\n    res.end('hello, world!')\n  })\n})\n```\n\n### write logs to a file\n\n#### single file\n\nSample app that will log all requests in the Apache combined format to the file\n`access.log`.\n\n```js\nvar express = require('express')\nvar fs = require('fs')\nvar morgan = require('morgan')\nvar path = require('path')\n\nvar app = express()\n\n// create a write stream (in append mode)\nvar accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })\n\n// setup the logger\napp.use(morgan('combined', { stream: accessLogStream }))\n\napp.get('/', function (req, res) {\n  res.send('hello, world!')\n})\n```\n\n#### log file rotation\n\nSample app that will log all requests in the Apache combined format to one log\nfile per day in the `log/` directory using the\n[rotating-file-stream module](https://www.npmjs.com/package/rotating-file-stream).\n\n```js\nvar express = require('express')\nvar morgan = require('morgan')\nvar path = require('path')\nvar rfs = require('rotating-file-stream') // version 2.x\n\nvar app = express()\n\n// create a rotating write stream\nvar accessLogStream = rfs.createStream('access.log', {\n  interval: '1d', // rotate daily\n  path: path.join(__dirname, 'log')\n})\n\n// setup the logger\napp.use(morgan('combined', { stream: accessLogStream }))\n\napp.get('/', function (req, res) {\n  res.send('hello, world!')\n})\n```\n\n### split / dual logging\n\nThe `morgan` middleware can be used as many times as needed, enabling\ncombinations like:\n\n  * Log entry on request and one on response\n  * Log all requests to file, but errors to console\n  * ... and more!\n\nSample app that will log all requests to a file using Apache format, but\nerror responses are logged to the console:\n\n```js\nvar express = require('express')\nvar fs = require('fs')\nvar morgan = require('morgan')\nvar path = require('path')\n\nvar app = express()\n\n// log only 4xx and 5xx responses to console\napp.use(morgan('dev', {\n  skip: function (req, res) { return res.statusCode < 400 }\n}))\n\n// log all requests to access.log\napp.use(morgan('common', {\n  stream: fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })\n}))\n\napp.get('/', function (req, res) {\n  res.send('hello, world!')\n})\n```\n\n### use custom token formats\n\nSample app that will use custom token formats. This adds an ID to all requests and displays it using the `:id` token.\n\n```js\nvar express = require('express')\nvar morgan = require('morgan')\nvar uuid = require('uuid')\n\nmorgan.token('id', function getId (req) {\n  return req.id\n})\n\nvar app = express()\n\napp.use(assignId)\napp.use(morgan(':id :method :url :response-time'))\n\napp.get('/', function (req, res) {\n  res.send('hello, world!')\n})\n\nfunction assignId (req, res, next) {\n  req.id = uuid.v4()\n  next()\n}\n```\n\n## License\n\n[MIT](LICENSE)\n\n[ci-image]: https://badgen.net/github/checks/expressjs/morgan/master?label=ci\n[ci-url]: https://github.com/expressjs/morgan/actions/workflows/ci.yml\n[coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/morgan/master\n[coveralls-url]: https://coveralls.io/r/expressjs/morgan?branch=master\n[npm-downloads-image]: https://badgen.net/npm/dm/morgan\n[npm-url]: https://npmjs.org/package/morgan\n[npm-version-image]: https://badgen.net/npm/v/morgan\n","publish_time":1789117062376,"_cnpm_publish_time":1789117062376,"dist-tags":{"latest":"1.12.1"}}