Name: express-winston-middleware
Owner: Formidable
Description: Winston log wrappers for Express.
Created: 2013-12-01 17:18:04.0
Updated: 2018-01-09 18:35:02.0
Pushed: 2018-01-09 18:35:01.0
Homepage: http://stack.formidable.com/express-winston-middleware/
Size: 42
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Winston log wrappers and middleware for Express.
You can install express-winston-middleware
using NPM:
m install express-winston-middleware
From there, you can create a Log
instance or use the request
Express
middleware.
See the examples
directory for some basic use cases.
The Log
logger class internally creates and wraps a Winston logger. You
can create one with:
winston = require("winston");
Log = require("express-winston-middleware").Log;
reate logger with Console transport and "foo" metadata item.
log = new Log({
ansports: [
new (winston.transports.Console)({ json: true })
Metadata to add to each log response.
o: "bar"
og something.
info("Hello World!");
which produces the following output:
ate": "2013-12-01T23:29:48.035Z",
nv": "development",
erver": {
"id": "m",
"pid": 24638,
"hostName": "titan.local"
oo": "bar",
evel": "info",
essage": "Hello World!"
The request
middleware is added to your Express setup like:
express = require("express");
app = express(),
winMid = require("express-winston-middleware");
.. */
ame options and meta as for the `Log` class.
use(new winMid.request({
ansports: [
new (winston.transports.Console)({ json: true })
Metadata to add to each log response.
o: "bar"
;
and produces output for requests like:
ate": "2013-12-01T23:32:54.759Z",
erver": {
"id": "m",
"pid": 24653,
"hostName": "titan.local"
eq": {
"method": "GET",
"host": "localhost:2000",
"path": "/",
"query": ""
es": {
"statusCode": 304
oo": "bar",
evel": "info",
essage": "request"
The middleware attaches a logger to the response locals,
available as res.locals._log
, so that in addition to automatic
request logging messages you can log extra messages with all of the
current request metadata. E.g.:
get("/foo", function (req, res) {
s.locals._log.info("This is an extra manual log message!", {
extra: "metadata"
;
Rest of your code here...
request(opts, baseMeta)
- Express request middlewareerror(opts, baseMeta)
- Express error middlewareuncaught(opts, baseMeta)
- Global uncaught exception handlerLog(opts, baseMeta)
- Logger class.Log.addMeta(meta)
Log.addReq(req)
Log.transformMeta(fn)
Log.addRes(res)
Log.addError(err)
request(opts, baseMeta)
- Express request middlewareCreates a middleware function using base metadata. Integration:
use(winMid.request({
ansports: [ new (winston.transports.Console)({ json: true }) ]
foo: "bar" }));
Once integrated, a logger will be attached to the response locals,
and available as res.locals._log
. The logger will then be removed at
the end of the request.
error(opts, baseMeta)
- Express error middlewareCreates a middleware function for Express. Integration:
use(winMid.error({
ansports: [ new (winston.transports.Console)({ json: true }) ]
foo: "bar" }));
uncaught(opts, baseMeta)
- Global uncaught exception handlerCreates a handler function for any uncaught exception. Integration:
ess.on("uncaughtException", winMid.uncaught({
ansports: [ new (winston.transports.Console)({ json: true }) ]
foo: "bar" }));
Note: Terminates process at end.
Log(opts, baseMeta)
- Logger class.Wraps Winston logger with additional functionality.
log = new winMid.Log({
ansports: [ new (winston.transports.Console)({ json: true }) ]
foo: "bar" }));
Log.addMeta(meta)
Add arbitrary meta to all subsequent log statements.
Log.addReq(req)
Add request to meta.
Log.transformMeta(fn)
Set a delayed single transform function to mutate a copy of the metadata right before a logging event. You can only presently have one such function. And it is delayed so that for things like request end, you can effectively access all the metadata.
The transform is applied on each log call and passes a copy of the mutated metadata to the actual log call.
The function signature should be fn(existingMeta)
and return mutated
metadata.
Log.addRes(res)
Add response to meta.
Log.addError(err)
Add error to meta.
Please see the Contributions Guide for how to help out with the plugin.
We test all changes with Travis CI. Here's our current build status:
All code is 2013-2016 Formidable Labs. Released under the MIT License.