express-errorlog
Advanced tools
Comparing version 0.0.1 to 1.0.1
@@ -7,3 +7,3 @@ module.exports = function(grunt) { | ||
all: { | ||
src: ['test/**/*.js'] | ||
src: ['test/**/*.test.js'] | ||
} | ||
@@ -10,0 +10,0 @@ } |
{ | ||
"name": "express-errorlog", | ||
"description": "Error handling and logging for Express (4.x)", | ||
"version": "0.0.1", | ||
"version": "1.0.1", | ||
"author": { | ||
@@ -9,3 +9,3 @@ "name": "Pier Fumagalli", | ||
}, | ||
"main": "src/index.js", | ||
"main": "src/express-errorlog.js", | ||
"license": "MIT", | ||
@@ -32,4 +32,5 @@ "repository": { | ||
"dependencies": { | ||
"errorlog": "^1.0.1", | ||
"statuses": "^1.2.1" | ||
} | ||
} |
301
README.md
Express Error Log | ||
================= | ||
A very simple logger for [Express](http://expressjs.com/) 4.x. | ||
A very simple logger for [Express](http://expressjs.com/) 4.x, based on | ||
the [`errorlog`](https://www.npmjs.com/package/errorlog) NPM module. | ||
* [Install and use](#install-and-use) | ||
* [Logging and responses](#logging-and-responses) | ||
* [Numbers](#numbers) | ||
* [Strings](#strings) | ||
* [Objects](#objects) | ||
* [Errors](#errors) | ||
* [Request IDs](#request-ids) | ||
* [Configuration](#configuration) | ||
* [Sample Output](#sample-output) | ||
* [License (MIT)](#license-mit-) | ||
Install and use | ||
@@ -27,39 +35,84 @@ --------------- | ||
In the code, various patterns can be used: | ||
### Direct response statuses | ||
Logging and responses | ||
--------------------- | ||
In order to trigger log entries, simply use Express' own `next(...)` function, | ||
passing one of the the following types of parameter: | ||
#### Numbers | ||
```javascript | ||
app.route('/somewhere', function(req, res, next) { | ||
next(404); // Simply interrupt with a 404 not found | ||
app.get('/test', function(req, res, next) { | ||
next(400); // Simply interrupt with a 400 Bad Request | ||
}); | ||
``` | ||
### Status, message and details | ||
The number will be interpreted as the status code of the response. If the number | ||
is less than zero or greater than 599, the response status will be normalized to | ||
a _500 Internal Server Error_. | ||
The response sent back to the client will contain the following: | ||
```json | ||
HTTP/1.1 400 Bad Request | ||
Content-Type: application/json | ||
{ | ||
"status": 400, | ||
"message": "Bad Request" | ||
} | ||
``` | ||
And the log will written with | ||
```text | ||
2015-03-30T16:45:01.661Z - GET /test (400) - Bad Request | ||
``` | ||
#### Strings | ||
```javascript | ||
app.route('/somewhere', function(req, res, next) { | ||
next({ | ||
status: 404, | ||
message: 'This will override the message', | ||
details: { // This will appear in the response | ||
key: 'value', | ||
any: [ 'sort', 'of', stuff ] | ||
} | ||
}); | ||
app.get('/test', function(req, res, next) { | ||
next("Something is wrong"); // Interrupt with a message | ||
}); | ||
``` | ||
### Errors to be logged only | ||
The number will be interpreted as the status message for the response, while the | ||
status will be defaulted to a _500 Internal Server Error_. | ||
The response sent back to the client will contain the following: | ||
```json | ||
HTTP/1.1 500 Internal Server Error | ||
Content-Type: application/json | ||
{ | ||
"status": 500, | ||
"message": "Something is wrong" | ||
} | ||
``` | ||
And the log will written with | ||
```text | ||
2015-03-30T16:45:01.661Z - GET /test (500) - Something is wrong | ||
``` | ||
#### Objects | ||
```javascript | ||
app.route('/somewhere', function(req, res, next) { | ||
app.get('/test', function(req, res, next) { | ||
var error = new Error('Invalid access for user'); | ||
error.user = 'pier@usrz.com'; | ||
error.token = 'c568019d-3c80-4685-8982-ed455a2c0cd1'; | ||
next({ | ||
status: 404, | ||
message: 'This will override the message', | ||
details: { // This will appear in the response | ||
key: 'value', | ||
any: [ 'sort', 'of', stuff ] | ||
status: 403, | ||
message: 'You have no access, my friend', | ||
error: error, | ||
details: { | ||
example: "... some extra token for the response ..." | ||
}, | ||
error: new Error("This will not be transmitted to the client") | ||
}); | ||
@@ -69,5 +122,104 @@ }); | ||
Objects can also be passed directly to the `next(...)` call, having the | ||
following keys: | ||
* `status`: A _number_ representing the status code of the response. | ||
* `message`: The message to transmit to the client. | ||
* `error`: An `Error` that will be logged, but not transmitted to the client. | ||
* `details`: Anything that will be serialized to JSON and sent back alongside | ||
the response. | ||
In the example above, the response will be: | ||
```json | ||
HTTP/1.1 403 Forbidden | ||
Content-Type: application/json | ||
{ | ||
"status": 403, | ||
"message": "You have no access, my friend", | ||
"details": { | ||
"example": "... some extra token for the response ..." | ||
} | ||
} | ||
``` | ||
And the log will contain something along the following: | ||
```text | ||
2015-03-30T16:45:01.718Z - GET /test (403) - You have no access, my friend | ||
>>> {"example":"... some extra token for the response ..."} | ||
>>> {"user":"pier@usrz.com","token":"c568019d-3c80-4685-8982-ed455a2c0cd1"} | ||
Error: Invalid access for user | ||
at Error (native) | ||
at ... stacktrace continues ... | ||
``` | ||
In other words, `details` and `error` will be logged and the `Error`'s stack | ||
trace will be dumped in full. At the same time, note that there is no trace of | ||
the `error` in the response sent back to the client. | ||
#### Errors | ||
```javascript | ||
app.get('/test', function(req, res, next) { | ||
// Create and instrument an error | ||
var error = new Error('Something is amiss'); | ||
error.status = 410; | ||
error.details = { | ||
example: "... some extra token for the response ..." | ||
}; | ||
// Equivalent to `next(error);` | ||
throw error; | ||
}); | ||
``` | ||
Whether thrown or passed to `next(...)`, exceptions will produce a _500 Internal | ||
Server Error_ unless they expose a `status` property directly and their message | ||
will be sent alongside the response, as: | ||
```json | ||
HTTP/1.1 410 Gone | ||
Content-Type: application/json | ||
{ | ||
"status": 410, | ||
"message": "Something is amiss", | ||
"details": { | ||
"example": "... some extra token for the response ..." | ||
} | ||
} | ||
``` | ||
The log will contain the full details and stack trace of the error: | ||
```text | ||
2015-03-30T16:45:01.718Z - GET /test (410) - Something is amiss | ||
>>> {"example":"... some extra token for the response ..."} | ||
Error: Something is amiss | ||
at Error (native) | ||
at ... stacktrace continues ... | ||
``` | ||
Request IDs | ||
----------- | ||
If the Express' `request` contains the special `id` value (as for example when | ||
using [`express-request-id`](https://www.npmjs.com/package/express-request-id)) | ||
said `id` will also be reported, for example: | ||
```text | ||
2015-03-30T16:45:01.661Z - d7c32387-3feb-452b-8df1-2d8338b3ea22 - GET /test (500) - Something is wrong | ||
``` | ||
Configuration | ||
============= | ||
------------- | ||
Configure accepts basically the same options as | ||
[`errorlog`](https://www.npmjs.com/package/errorlog): | ||
```javascript | ||
@@ -81,86 +233,31 @@ var errorlog = require('express-errorlog'); | ||
* `logger`: A `function` receiving the message to be logged or a `stream` that | ||
will be written to with the date and error message. | ||
* `logger` may be one of the following: | ||
* a `Writable` _stream_ to which error messages will be written to (actually | ||
an object offering a `write(...)` function will do). | ||
* a simple `function` that will be invoked once with each message to log. | ||
* if unspecified this will default to `process.stderr`. | ||
* `category`: a category name that will be inserted in the message to log. | ||
* `render`: A _boolean_, if `true` the response will be sent to the client | ||
using Express' own `render(...)` function. | ||
using Express' own `render(...)` function (extra for `express-errorlog`). | ||
For log rotation use something similar to [`logrotate-stream`](https://www.npmjs.com/package/logrotate-stream). | ||
As with [`errorlog`](https://www.npmjs.com/package/errorlog), use a package | ||
like [`logrotate-stream`](https://www.npmjs.com/package/logrotate-stream) if | ||
log rotation is necessary in your environment. | ||
Sample Output | ||
============= | ||
Here is some sample output derived from the tests: | ||
```text | ||
2015-03-30T16:45:01.661Z - GET /test-1 (400) - Bad Request | ||
2015-03-30T16:45:01.682Z - GET /test-2 (499) - Unknown status 499 | ||
2015-03-30T16:45:01.689Z - GET /test-3 (500) - Unknown status 999 | ||
2015-03-30T16:45:01.694Z - GET /test-4 (401) - message for test-4 | ||
2015-03-30T16:45:01.699Z - GET /test-5 (402) - Payment Required | ||
>>> { testname: 'test-5' } | ||
2015-03-30T16:45:01.704Z - GET /test-6 (403) - message for test-6 | ||
>>> { testname: 'test-6' } | ||
2015-03-30T16:45:01.707Z - GET /test-7 (500) - message for test-7 | ||
>>> { testname: 'test-7' } | ||
2015-03-30T16:45:01.711Z - GET /test-8 (500) - exception message for test-8 | ||
>>> [Error: exception message for test-8] | ||
Error: exception message for test-8 | ||
at /Users/pier/Workspace/js-express-errorlog/test/test.js:13:57 | ||
at Layer.handle [as handle_request] (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/layer.js:82:5) | ||
at next (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/route.js:110:13) | ||
at Route.dispatch (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/route.js:91:3) | ||
at Layer.handle [as handle_request] (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/layer.js:82:5) | ||
at /Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:267:22 | ||
at Function.proto.process_params (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:321:12) | ||
at next (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:261:10) | ||
at expressInit (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/middleware/init.js:23:5) | ||
at Layer.handle [as handle_request] (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/layer.js:82:5) | ||
2015-03-30T16:45:01.715Z - GET /test-9 (410) - exception message for test-9 | ||
>>> { [Error: exception message for test-9] | ||
status: 410, | ||
details: { testname: 'test-9' }, | ||
extra: 'this only gets logged!' } | ||
Error: exception message for test-9 | ||
at Error (native) | ||
at /Users/pier/Workspace/js-express-errorlog/test/test.js:16:15 | ||
at Layer.handle [as handle_request] (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/layer.js:82:5) | ||
at next (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/route.js:110:13) | ||
at Route.dispatch (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/route.js:91:3) | ||
at Layer.handle [as handle_request] (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/layer.js:82:5) | ||
at /Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:267:22 | ||
at Function.proto.process_params (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:321:12) | ||
at next (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:261:10) | ||
at expressInit (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/middleware/init.js:23:5) | ||
2015-03-30T16:45:01.718Z - GET /test-0 (411) - message for test-0 | ||
>>> { testname: 'test-0' } | ||
>>> { [Error: exception message for test-0] | ||
more1: 'some more details for test 0', | ||
more2: 'even more details for test 0' } | ||
Error: exception message for test-0 | ||
at Error (native) | ||
at /Users/pier/Workspace/js-express-errorlog/test/test.js:24:15 | ||
at Layer.handle [as handle_request] (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/layer.js:82:5) | ||
at next (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/route.js:110:13) | ||
at Route.dispatch (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/route.js:91:3) | ||
at Layer.handle [as handle_request] (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/layer.js:82:5) | ||
at /Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:267:22 | ||
at Function.proto.process_params (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:321:12) | ||
at next (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/router/index.js:261:10) | ||
at expressInit (/Users/pier/Workspace/js-express-errorlog/node_modules/express/lib/middleware/init.js:23:5) | ||
``` | ||
License (MIT) | ||
============= | ||
------------- | ||
Copyright (c) 2015 Pier Fumagalli and USRZ.com | ||
Copyright (c) 2015 USRZ.com and Pier Paolo Fumagalli | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
@@ -172,4 +269,4 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
269
20926
2
352
1
+ Addederrorlog@^1.0.1
+ Addederrorlog@1.4.7(transitive)