Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. # morgan
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Build Status][travis-image]][travis-url]
  5. [![Test Coverage][coveralls-image]][coveralls-url]
  6. HTTP request logger middleware for node.js
  7. > Named after [Dexter](http://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.
  8. ## API
  9. <!-- eslint-disable no-unused-vars -->
  10. ```js
  11. var morgan = require('morgan')
  12. ```
  13. ### morgan(format, options)
  14. Create a new morgan logger middleware function using the given `format` and `options`.
  15. The `format` argument may be a string of a predefined name (see below for the names),
  16. a string of a format string, or a function that will produce a log entry.
  17. The `format` function will be called with three arguments `tokens`, `req`, and `res`,
  18. where `tokens` is an object with all defined tokens, `req` is the HTTP request and `res`
  19. is the HTTP response. The function is expected to return a string that will be the log
  20. line, or `undefined` / `null` to skip logging.
  21. #### Using a predefined format string
  22. <!-- eslint-disable no-undef -->
  23. ```js
  24. morgan('tiny')
  25. ```
  26. #### Using format string of predefined tokens
  27. <!-- eslint-disable no-undef -->
  28. ```js
  29. morgan(':method :url :status :res[content-length] - :response-time ms')
  30. ```
  31. #### Using a custom format function
  32. <!-- eslint-disable no-undef -->
  33. ``` js
  34. morgan(function (tokens, req, res) {
  35. return [
  36. tokens.method(req, res),
  37. tokens.url(req, res),
  38. tokens.status(req, res),
  39. tokens.res(req, res, 'content-length'), '-',
  40. tokens['response-time'](req, res), 'ms'
  41. ].join(' ')
  42. })
  43. ```
  44. #### Options
  45. Morgan accepts these properties in the options object.
  46. ##### immediate
  47. Write log line on request instead of response. This means that a requests will
  48. be logged even if the server crashes, _but data from the response (like the
  49. response code, content length, etc.) cannot be logged_.
  50. ##### skip
  51. Function to determine if logging is skipped, defaults to `false`. This function
  52. will be called as `skip(req, res)`.
  53. <!-- eslint-disable no-undef -->
  54. ```js
  55. // EXAMPLE: only log error responses
  56. morgan('combined', {
  57. skip: function (req, res) { return res.statusCode < 400 }
  58. })
  59. ```
  60. ##### stream
  61. Output stream for writing log lines, defaults to `process.stdout`.
  62. #### Predefined Formats
  63. There are various pre-defined formats provided:
  64. ##### combined
  65. Standard Apache combined log output.
  66. ```
  67. :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
  68. ```
  69. ##### common
  70. Standard Apache common log output.
  71. ```
  72. :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]
  73. ```
  74. ##### dev
  75. Concise output colored by response status for development use. The `:status`
  76. token will be colored red for server error codes, yellow for client error
  77. codes, cyan for redirection codes, and uncolored for all other codes.
  78. ```
  79. :method :url :status :response-time ms - :res[content-length]
  80. ```
  81. ##### short
  82. Shorter than default, also including response time.
  83. ```
  84. :remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms
  85. ```
  86. ##### tiny
  87. The minimal output.
  88. ```
  89. :method :url :status :res[content-length] - :response-time ms
  90. ```
  91. #### Tokens
  92. ##### Creating new tokens
  93. To define a token, simply invoke `morgan.token()` with the name and a callback function.
  94. This callback function is expected to return a string value. The value returned is then
  95. available as ":type" in this case:
  96. <!-- eslint-disable no-undef -->
  97. ```js
  98. morgan.token('type', function (req, res) { return req.headers['content-type'] })
  99. ```
  100. Calling `morgan.token()` using the same name as an existing token will overwrite that
  101. token definition.
  102. The token function is expected to be called with the arguments `req` and `res`, representing
  103. the HTTP request and HTTP response. Additionally, the token can accept further arguments of
  104. it's choosing to customize behavior.
  105. ##### :date[format]
  106. The current date and time in UTC. The available formats are:
  107. - `clf` for the common log format (`"10/Oct/2000:13:55:36 +0000"`)
  108. - `iso` for the common ISO 8601 date time format (`2000-10-10T13:55:36.000Z`)
  109. - `web` for the common RFC 1123 date time format (`Tue, 10 Oct 2000 13:55:36 GMT`)
  110. If no format is given, then the default is `web`.
  111. ##### :http-version
  112. The HTTP version of the request.
  113. ##### :method
  114. The HTTP method of the request.
  115. ##### :referrer
  116. The Referrer header of the request. This will use the standard mis-spelled Referer header if exists, otherwise Referrer.
  117. ##### :remote-addr
  118. The remote address of the request. This will use `req.ip`, otherwise the standard `req.connection.remoteAddress` value (socket address).
  119. ##### :remote-user
  120. The user authenticated as part of Basic auth for the request.
  121. ##### :req[header]
  122. The given `header` of the request. If the header is not present, the
  123. value will be displayed as `"-"` in the log.
  124. ##### :res[header]
  125. The given `header` of the response. If the header is not present, the
  126. value will be displayed as `"-"` in the log.
  127. ##### :response-time[digits]
  128. The time between the request coming into `morgan` and when the response
  129. headers are written, in milliseconds.
  130. The `digits` argument is a number that specifies the number of digits to
  131. include on the number, defaulting to `3`, which provides microsecond precision.
  132. ##### :status
  133. The status code of the response.
  134. If the request/response cycle completes before a response was sent to the
  135. client (for example, the TCP socket closed prematurely by a client aborting
  136. the request), then the status will be empty (displayed as `"-"` in the log).
  137. ##### :url
  138. The URL of the request. This will use `req.originalUrl` if exists, otherwise `req.url`.
  139. ##### :user-agent
  140. The contents of the User-Agent header of the request.
  141. ### morgan.compile(format)
  142. Compile a format string into a `format` function for use by `morgan`. A format string
  143. is a string that represents a single log line and can utilize token syntax.
  144. Tokens are references by `:token-name`. If tokens accept arguments, they can
  145. be passed using `[]`, for example: `:token-name[pretty]` would pass the string
  146. `'pretty'` as an argument to the token `token-name`.
  147. The function returned from `morgan.compile` takes three arguments `tokens`, `req`, and
  148. `res`, where `tokens` is object with all defined tokens, `req` is the HTTP request and
  149. `res` is the HTTP response. The function will return a string that will be the log line,
  150. or `undefined` / `null` to skip logging.
  151. Normally formats are defined using `morgan.format(name, format)`, but for certain
  152. advanced uses, this compile function is directly available.
  153. ## Examples
  154. ### express/connect
  155. Simple app that will log all request in the Apache combined format to STDOUT
  156. ```js
  157. var express = require('express')
  158. var morgan = require('morgan')
  159. var app = express()
  160. app.use(morgan('combined'))
  161. app.get('/', function (req, res) {
  162. res.send('hello, world!')
  163. })
  164. ```
  165. ### vanilla http server
  166. Simple app that will log all request in the Apache combined format to STDOUT
  167. ```js
  168. var finalhandler = require('finalhandler')
  169. var http = require('http')
  170. var morgan = require('morgan')
  171. // create "middleware"
  172. var logger = morgan('combined')
  173. http.createServer(function (req, res) {
  174. var done = finalhandler(req, res)
  175. logger(req, res, function (err) {
  176. if (err) return done(err)
  177. // respond to request
  178. res.setHeader('content-type', 'text/plain')
  179. res.end('hello, world!')
  180. })
  181. })
  182. ```
  183. ### write logs to a file
  184. #### single file
  185. Simple app that will log all requests in the Apache combined format to the file
  186. `access.log`.
  187. ```js
  188. var express = require('express')
  189. var fs = require('fs')
  190. var morgan = require('morgan')
  191. var path = require('path')
  192. var app = express()
  193. // create a write stream (in append mode)
  194. var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
  195. // setup the logger
  196. app.use(morgan('combined', { stream: accessLogStream }))
  197. app.get('/', function (req, res) {
  198. res.send('hello, world!')
  199. })
  200. ```
  201. #### log file rotation
  202. Simple app that will log all requests in the Apache combined format to one log
  203. file per day in the `log/` directory using the
  204. [rotating-file-stream module](https://www.npmjs.com/package/rotating-file-stream).
  205. ```js
  206. var express = require('express')
  207. var fs = require('fs')
  208. var morgan = require('morgan')
  209. var path = require('path')
  210. var rfs = require('rotating-file-stream')
  211. var app = express()
  212. var logDirectory = path.join(__dirname, 'log')
  213. // ensure log directory exists
  214. fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
  215. // create a rotating write stream
  216. var accessLogStream = rfs('access.log', {
  217. interval: '1d', // rotate daily
  218. path: logDirectory
  219. })
  220. // setup the logger
  221. app.use(morgan('combined', { stream: accessLogStream }))
  222. app.get('/', function (req, res) {
  223. res.send('hello, world!')
  224. })
  225. ```
  226. ### split / dual logging
  227. The `morgan` middleware can be used as many times as needed, enabling
  228. combinations like:
  229. * Log entry on request and one on response
  230. * Log all requests to file, but errors to console
  231. * ... and more!
  232. Sample app that will log all requests to a file using Apache format, but
  233. error responses are logged to the console:
  234. ```js
  235. var express = require('express')
  236. var fs = require('fs')
  237. var morgan = require('morgan')
  238. var path = require('path')
  239. var app = express()
  240. // log only 4xx and 5xx responses to console
  241. app.use(morgan('dev', {
  242. skip: function (req, res) { return res.statusCode < 400 }
  243. }))
  244. // log all requests to access.log
  245. app.use(morgan('common', {
  246. stream: fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
  247. }))
  248. app.get('/', function (req, res) {
  249. res.send('hello, world!')
  250. })
  251. ```
  252. ### use custom token formats
  253. Sample app that will use custom token formats. This adds an ID to all requests and displays it using the `:id` token.
  254. ```js
  255. var express = require('express')
  256. var morgan = require('morgan')
  257. var uuid = require('node-uuid')
  258. morgan.token('id', function getId (req) {
  259. return req.id
  260. })
  261. var app = express()
  262. app.use(assignId)
  263. app.use(morgan(':id :method :url :response-time'))
  264. app.get('/', function (req, res) {
  265. res.send('hello, world!')
  266. })
  267. function assignId (req, res, next) {
  268. req.id = uuid.v4()
  269. next()
  270. }
  271. ```
  272. ## License
  273. [MIT](LICENSE)
  274. [npm-image]: https://img.shields.io/npm/v/morgan.svg
  275. [npm-url]: https://npmjs.org/package/morgan
  276. [travis-image]: https://img.shields.io/travis/expressjs/morgan/master.svg
  277. [travis-url]: https://travis-ci.org/expressjs/morgan
  278. [coveralls-image]: https://img.shields.io/coveralls/expressjs/morgan/master.svg
  279. [coveralls-url]: https://coveralls.io/r/expressjs/morgan?branch=master
  280. [downloads-image]: https://img.shields.io/npm/dm/morgan.svg
  281. [downloads-url]: https://npmjs.org/package/morgan