Logging middleware for node.js http apps, which emits LWES events instead of logging to disk.
Based on as forked from morgan.
var express = require('express');
var emitter = require('morgan-lwes');
var app = express();
app.use(emitter());
This is a sample event when using the setup above.
{ type: 'MorganLWES::Logger',
attributes:
{ status: '200',
referrer: '-',
user_agent: 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0',
response_time: '0.683',
response_content_length: '12',
method: 'GET',
remote_addr: '127.0.0.1',
date: 'Wed, 11 Jun 2014 23:00:37 GMT',
url: '/',
http_version: '1.1' } }
{ type: 'MorganLWES::Logger',
attributes:
{ status: '404',
referrer: '-',
user_agent: 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0',
response_time: '0.265',
response_content_length: '-',
method: 'GET',
remote_addr: '127.0.0.1',
date: 'Wed, 11 Jun 2014 23:00:37 GMT',
url: '/favicon.ico',
http_version: '1.1' } }
MorganLWES may be passed options to configure the tokens which are emitted. The options may be passed as a predefined list of tokens, token list, or function.
morgan() // default
morgan('short')
morgan('tiny')
morgan({ format: 'short', immediate: true })
morgan([':method', ':url', ':referrer'])
morgan([':request[content-type]', ':response[content-type]'])
morgan(function(tokens, req, res){ return { data: 'some format string', status: res.statusCode } });
morgan({ format: 'tiny', skip: function(req, res){ return res.statusCode === 304; }});
default
- Standard output.short
- Shorter than default, also including response time.tiny
- The minimal.
MorganLWES accepts these properties in the options object.
format
- Format string or Setting, see below for format tokens.emitter
- Custom emitter, defaults to built in emitter. (Mostly for testing.)lwes
- Object containing lwes configurations fornew liblwes.Emitter(...)
.type
- Emittedtype
field, defaultMorganLWES::Logger
.address
- Address to emit on, default127.0.0.1
.port
- Port to emit on, defaults to1111
.esf
- Path on disk to ESF file, defaults toundefined
.
immediate
- Write log line on request instead of response (for response times).skip
- Function to determine if logging is skipped, called asskip(req, res)
, defaults tofalse
.
All default formats are defined this way, however the api is also public:
morgan.format('name', 'array or function')
:request[header]
ex::request[Accept]
:response[header]
ex::response[Content-Length]
:http-version
:response-time
:remote-addr
:date
:method
:url
:referrer
:user-agent
:status
To define a token, simply invoke morgan.token()
with the name and a callback function. The value returned is then available as ":type" in this case:
morgan.token('type', function(req, res){ return req.headers['content-type']; })
- Add
esf
examples for preformatted setting -- default, short and tiny.
The MIT License (MIT)
Copyright (c) 2014 Joshua P. Mervine [email protected]
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 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.