Reformat project into workspaces
This commit is contained in:
1 parent
20bb10114b
commit
c3e2cf5ed3
38 files changed
+4863
-4186
No files matched your search
+40
-1
@@ -7,7 +7,7 @@
|
||||
|
||||
HTTP request logger middleware for node.js
|
||||
|
||||
> Named after [Dexter](http://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.
|
||||
> Named after [Dexter](https://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -21,6 +21,16 @@ $ npm install morgan
|
||||
|
||||
## API
|
||||
|
||||
For ES Modules:
|
||||
|
||||
<!-- eslint-disable no-unused-vars -->
|
||||
|
||||
```js
|
||||
import morgan from 'morgan'
|
||||
```
|
||||
|
||||
For CommonJS:
|
||||
|
||||
<!-- eslint-disable no-unused-vars -->
|
||||
|
||||
```js
|
||||
@@ -98,6 +108,30 @@ morgan('combined', {
|
||||
|
||||
Output stream for writing log lines, defaults to `process.stdout`.
|
||||
|
||||
If the stream is in object mode (`stream.writableObjectMode` is `true`) and the
|
||||
format function returns an object, the object is written to the stream as-is,
|
||||
without a trailing newline. This allows passing structured log entries to
|
||||
loggers that accept objects:
|
||||
|
||||
<!-- eslint-disable no-undef -->
|
||||
|
||||
```js
|
||||
morgan(function (tokens, req, res) {
|
||||
return {
|
||||
method: tokens.method(req, res),
|
||||
url: tokens.url(req, res),
|
||||
status: Number(tokens.status(req, res))
|
||||
}
|
||||
}, {
|
||||
stream: {
|
||||
writableObjectMode: true,
|
||||
write: function (entry) {
|
||||
// entry is the object returned by the format function
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### Predefined Formats
|
||||
|
||||
There are various pre-defined formats provided:
|
||||
@@ -134,6 +168,11 @@ for information codes.
|
||||
GET /dev 200 0.224 ms - 2
|
||||
```
|
||||
|
||||
Coloring is disabled when the [`NO_COLOR`](https://no-color.org) environment
|
||||
variable is set to any non-empty value, in which case the output above is
|
||||
written without any escape sequences. The variable is read once, when `morgan`
|
||||
is first required.
|
||||
|
||||
##### short
|
||||
|
||||
Shorter than default, also including response time.
|
||||
|
||||
+22
-5
@@ -47,6 +47,8 @@ var CLF_MONTH = [
|
||||
|
||||
var DEFAULT_BUFFER_DURATION = 1000
|
||||
|
||||
var NO_COLOR = Boolean(process.env.NO_COLOR)
|
||||
|
||||
/**
|
||||
* Escape control characters and backslashes so a value is safe for
|
||||
* line-oriented logs.
|
||||
@@ -59,7 +61,7 @@ function escapeLogField (value) {
|
||||
if (value == null) return undefined
|
||||
|
||||
// eslint-disable-next-line no-control-regex
|
||||
return String(value).replace(/[\u0000-\u001f\u007f\\]/g, function (ch) {
|
||||
return String(value).replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029\\]/g, function (ch) {
|
||||
switch (ch) {
|
||||
case '\\': return '\\\\'
|
||||
case '\b': return '\\b'
|
||||
@@ -153,7 +155,11 @@ function morgan (format, options) {
|
||||
}
|
||||
|
||||
debug('log request')
|
||||
stream.write(line + '\n')
|
||||
if (stream.writableObjectMode && typeof line === 'object') {
|
||||
stream.write(line)
|
||||
} else {
|
||||
stream.write(line + '\n')
|
||||
}
|
||||
};
|
||||
|
||||
if (immediate) {
|
||||
@@ -231,6 +237,12 @@ morgan.format('dev', function developmentFormatLine (tokens, req, res) {
|
||||
return fn(tokens, req, res)
|
||||
})
|
||||
|
||||
// NO_COLOR (https://no-color.org): when set and not an empty string, the dev
|
||||
// format is replaced at load time with a variant free of escape sequences
|
||||
if (NO_COLOR) {
|
||||
morgan.format('dev', compile(':method :url :status :response-time ms - :res[content-length]'))
|
||||
}
|
||||
|
||||
/**
|
||||
* request url
|
||||
*/
|
||||
@@ -334,9 +346,9 @@ morgan.token('remote-user', function getRemoteUserToken (req) {
|
||||
// parse basic credentials
|
||||
var credentials = auth(req)
|
||||
|
||||
// return username
|
||||
// return username (escaping is applied by the token() wrapper)
|
||||
return credentials
|
||||
? escapeLogField(credentials.name)
|
||||
? credentials.name
|
||||
: undefined
|
||||
})
|
||||
|
||||
@@ -573,6 +585,11 @@ function recordStartTime () {
|
||||
*/
|
||||
|
||||
function token (name, fn) {
|
||||
morgan[name] = fn
|
||||
// wrap the token so its string output is always escaped for line-oriented
|
||||
// logs, regardless of whether the format is a string or a function
|
||||
morgan[name] = function tokenValue () {
|
||||
var value = fn.apply(this, arguments)
|
||||
return typeof value === 'string' ? escapeLogField(value) : value
|
||||
}
|
||||
return this
|
||||
}
|
||||
+6
-6
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "morgan",
|
||||
"description": "HTTP request logger middleware for node.js",
|
||||
"version": "1.11.0",
|
||||
"version": "1.12.0",
|
||||
"contributors": [
|
||||
"Douglas Christopher Wilson <doug@somethingdoug.com>",
|
||||
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
|
||||
@@ -28,12 +28,12 @@
|
||||
"devDependencies": {
|
||||
"eslint": "6.8.0",
|
||||
"eslint-config-standard": "14.1.1",
|
||||
"eslint-plugin-import": "2.20.2",
|
||||
"eslint-plugin-import": "2.32.0",
|
||||
"eslint-plugin-markdown": "1.0.2",
|
||||
"eslint-plugin-node": "11.1.0",
|
||||
"eslint-plugin-promise": "4.2.1",
|
||||
"eslint-plugin-standard": "4.0.1",
|
||||
"mocha": "10.4.0",
|
||||
"eslint-plugin-promise": "4.3.1",
|
||||
"eslint-plugin-standard": "4.1.0",
|
||||
"mocha": "10.8.2",
|
||||
"nyc": "15.1.0",
|
||||
"split": "1.0.1",
|
||||
"supertest": "4.0.2"
|
||||
@@ -48,7 +48,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint --plugin markdown --ext js,md .",
|
||||
"test": "mocha --check-leaks --reporter spec",
|
||||
"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"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user