Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting custom status levels #102

Merged
merged 1 commit into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Use `expressWinston.logger(options)` to create a middleware to log your HTTP req
meta: Boolean, // control whether you want to log the meta data about the request (default to true).
baseMeta: Object, // default meta data to be added to log, this will be merged with the meta data.
metaField: String, // if defined, the meta data will be added in this field instead of the meta root object.
statusLevels: Boolean // different HTTP status codes caused log messages to be logged at different levels (info/warn/error), the default is false
statusLevels: Boolean or Object // different HTTP status codes caused log messages to be logged at different levels (info/warn/error), the default is false. Use an object to control the levels various status codes are logged at.
ignoreRoute: function (req, res) { return false; } // allows to skip some log messages based on request and/or response.
skip: function(req, res) { return false; } // function to determine if logging is skipped, defaults to false.
requestFilter: function (req, propName) { return req[propName]; } // A function to filter/return request values, defaults to returning all values allowed by whitelist. If the function returns undefined, the key/value will not be included in the meta.
Expand Down Expand Up @@ -366,6 +366,17 @@ If both `req._bodyWhitelist.body` and `req._bodyBlacklist.body` are set the resu
excluding any black listed ones. In the above example, only 'email' and 'age' would be included.


## Custom Status Levels

If you set statusLevels to true express-winston will log sub 400 responses at info level, sub 500 responses as warnings and 500+ responses as errors. To chagne these levels specify an object as follows
```json
"statusLevels": {
"success": "debug",
"warn": "debug",
"error": "info"
}
```

## Tests

Run the basic Mocha tests:
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ function logger(options) {
req.url = req.originalUrl || req.url;

if (options.statusLevels) {
if (res.statusCode >= 100) { options.level = "info"; }
if (res.statusCode >= 400) { options.level = "warn"; }
if (res.statusCode >= 500) { options.level = "error"; }
if (res.statusCode >= 100) { options.level = options.statusLevels.success || "info"; }
if (res.statusCode >= 400) { options.level = options.statusLevels.warn || "warn"; }
if (res.statusCode >= 500) { options.level = options.statusLevels.error || "error"; }
};

if (options.colorStatus || options.expressFormat) {
Expand Down