expressjs / morgan Goto Github PK
View Code? Open in Web Editor NEWHTTP request logger middleware for node.js
License: MIT License
HTTP request logger middleware for node.js
License: MIT License
migrating from senchalabs/connect#700
I can't find the way to do that from morgan readme ?
In the line 12, morgan requires bytes
module, but never used.
Shall we use it for formatting 'Content-Length' header like this?
exports.token('res', function(req, res, field){
var result = (res._headers || {})[field.toLowerCase()];
if (result && field.toLowerCase() === 'content-length') {
var len = parseInt(result, 10);
return bytes(len);
}
else
return result;
});
I found this issue when I upgraded the express version.
The usual request loggers log after the request was responded, because at this point we have all the data: response time, return code, etc... This is what morgan
does by default.
But for debug purpose it's often useful to have a log entry when the request was just received, morgan
also provides this feature with the immediate
flag.
However, what we really need when debugging it immediate log and all the response information. The only way to implement this is to log two times: one like immediate, and one like the default.
We then need something to correlate both logs (a correlation id: a counter, or some other thing).
And finally: something to distinguish between both logs entries (a visual clue like a prefix).
Without this feature the immediate
feature is usually useless. Or am I missing a standard use-case?
I would like to change the format throughout runtime, is this a possibility in its current state?
ie change from 'dev' to 'combined' and the corresponding log output should be in combined format.
After 1.2.0 always empty next tokens
:response-time
:status
:remote-user
it seems morgan no longer support two arguments
This example in the readme is outdated...
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})
// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))
// shoud be
app.use(morgan({
format: 'combined',
stream: accessLogStream
}));
Could you please export the compile function so we can define custom and colored formats like the dev
one.
for example:
morgan.format('myformat', function(tokens, req, res){
var color = 32; // green
var status = res.statusCode;
if (status >= 500) color = 31; // red
else if (status >= 400) color = 33; // yellow
else if (status >= 300) color = 36; // cyan
var fn = morgan.compile('\x1b[0m:method :url \x1b[' + color + 'm:status \x1b[0m');
return fn(tokens, req, res);
});
app.use(morgan('myformat'));
Currently that's not possible because compile
is private
Great job on this module!
Is it possible to have logger/morgan output data through a bunyan logger? Would that be a desirable feature?
Hi, I was hoping you could help me out here.
I'm trying to log requests to a file. I'm using:
app.use(morgan('dev'));
app.use(morgan('common', {stream: fs.createWriteStream('./access.log', {flags: 'a'})}));
This creates a file called access.log in the root directory, I also have a 'common' flag in my console before each logged request. But, the file access.log remains empty. Is there anything I'm doing wrong here?
While the documentation says this about response-time
:
The time between the request coming into morgan and when the response headers are written, in milliseconds.
We have noticed that latent / far away clients record higher response times than closer ones, making this number difficult to use for any sort of performance monitoring.
It appears that the module on-finished
only calls back when the response has been completely written, acked and closed.
Am I correct on this? If so, would it make sense to write a PR to either fix response-time
or add another token that ignores client latency?
Many themes have trouble with the Grey ANSI color 90 that is being used in dev output.
I'm seeing this in Solarized Dark, but it is a problem in other themes as well.
Changing from 1b[90m
to 1b[92m
fixed the problem for me.
Last year Bower made a similar change for the same reason.
HOW TO Log GET/POST input params?
Is there an option to have the colour output like it was in express 3.0?
I tried to reopen the log files but I got 'Error: write after end'.
Does morgan have the function for reopening logger?
var logger = require('morgan');
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a' });
app.use((logger('dev', { stream: accessLogStream })));
process.on('SIGUSR2', function() {
console.log('Reopen the log files');
accessLogStream.removeAllListeners();
accessLogStream.end();
accessLogStream.close();
accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a' });
});
Reopen the log files
[Error: write after end]
[Error: write after end]
The token "response-time" is converted to a string. IMO the behavior should be consistent with other numeric tokens such as "status", that is a number.
To put it in context, I write logs as JSON, so the format is important and makes it much easier to further process logs (for example, calculate the average response time).
The change is easy using parseFloat
. If this sounds good, I can send a PR.
Is there a way to stop express from polluting the logs?
My configuration looks like this (but I have tried with all of the defaults):
var morgan = require('morgan');
...
app.use(morgan('dev'));
I get something like the following, for each request when I'm only really interested in seeing the last line. Is there a way that I can filter out all of the lines that contain "express:router"?
Sun, 27 Apr 2014 09:20:15 GMT express:router dispatching GET /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router query : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router expressInit : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router logger : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router anonymous : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router compress : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router cookieParser : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router session : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router anonymous : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router bodyParser : /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router trim prefix () from url /favicon.ico
Sun, 27 Apr 2014 09:20:15 GMT express:router staticMiddleware : /favicon.ico
GET /favicon.ico 304 2ms
Any feedback would be appreciated.
Hey there awesome dudes,
I LOVE THIS LIBRARY.
I just implemented socket.io on my server, and now I see this log all the stinking time:
Wed, 15 Apr 2015 15:27:14 GMT - GET /socket.io/?EIO=3&transport=polling&t=1429111634816-32&b64=1 responded 404 in 0.177 ms
Is there a way to ignore logs with a url that contains /socket.io
? Thanks!
I would like to use morgan with another logger. I'm doing this like so
app.use(morgan("combined",{
"stream": {
write: function(str) {
logger.debug(str);
}
}
}));
Instead of parsing the string, would it be possible to add a 'json' format to morgan?
line 124:
The HTTP version of the request.
should be
The HTTP method of the request.
I'm trying to get morgan to only print requests with a certain status code in conjunction with Express 4.
Docs say to do this:
// EXAMPLE: only log error responses
morgan('combined', {
skip: function (req, res) { return res.statusCode < 400 }
})
My code:
var express = require('express');
var morgan = require('morgan');
var app = express();
app.use(morgan('dev', {
skip: function(req, res) { return res.statusCode < 304 }
}));
It is logging in the expected format however it won't skip anything so appears to be completely ignoring the skip option.
Have even tried short circuiting the function to true
app.use(morgan('dev', {
skip: function (req, res) {
return true;
}
}
)
);
Is it possible to config morgan to write to syslog via udp?
I'm struggling to deploy my server to Heroku. Below you see the result of running node from the Heroku CLI, and trying to require
morgan. I'm getting the same issue in my logs and it is preventing the app from unless I comment out morgan.
> var morgan = require("morgan");
Error: Cannot find module 'debug'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/app/node_modules/morgan/index.js:16:13)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
Line 16 of index.js reads var debug = require('debug')('morgan')
Weirdly, everything runs fine on my development machine at home
I want change formatting in this logger ('dev') in Node.js + Express.js environment.
common usage:
var morgan= require('morgan');
// ...
app.use(morgan('dev'));
I need to change the formatting that I achieved the desired result:
What is good approach to achieve the desired result?
My solution is not clean, because I copied implementations of tokens from core of morgan logger and I added code for format result.
I overrided (or redefined) tokens by calling morgan.token():
morgan.token('url', function(req, res) {
var url = req.originalUrl || req.url,
length = 30; // length of final string
// format result:
return length < url.length ? url : ('' + url + repeatStr(' ', length - url.length));
});
morgan.token('response-time', function (req, res) {
if (!res._header || !req._startAt) return '';
var diff = process.hrtime(req._startAt);
var ms = diff[0] * 1e3 + diff[1] * 1e-6;
ms = ms.toFixed(3);
var timeLength = 8; // length of final string
// format result:
return ('' + ms).length > timeLength ? ms : repeatStr(' ', timeLength - ('' + ms).length) + ms;
});
this is helper function:
/**
* Example:
* input: repeadStr('-', 5)
* output: '-----'
*/
function repeatStr(str, count) {
var finalStr = '' + str;
for (var i = 0; i < count; i++) {
finalStr += str;
}
return finalStr;
}
Thanks for advices.
app.use(function (req, res, next) {
req.id = 0;
next();
});
app.use(morgan(':id'));
...
This will log -
instead of 0
, because I think somewhere in morgan
there is something like:
value = value || '-'
which is not strict enough in this case.
I would go with something like: (to discuss)
if (value === undefined || value === null) { value = '-' }
Hi!
Given this code:
.get('/test', function(req,res){
res.status(200).json({message:'everything ok'})
})
How can I log the message with morgan?
morgan.token('json', function(req, res){ return res.body; })
app.use(morgan(':method :url :status :response-time ms - :res[content-length] :json'))
res.body is empty ?_?
I am trying for hours now, sorry if this is a stupid question.
The date string in the logger output is in UTC. Why not output the date in local time zone? I.e., change this line to
return new Date().toString();
migrating from senchalabs/connect#333
What is the best way to write out JSON to stdout
? For example:
{
"date": "2015-05-30T13:55:36.000Z",
"http_version": "1.1",
"method": "GET",
"url": "/",
"status": 200,
"response_time": 75.564,
"remote_user": null,
"remote_addr": "43.34.54.121",
"user_agent": "curl/7.37.1",
"referrer": null
}
Hi,
:date[iso] logs Wed, 19 Nov 2014 02:04:32 GMT which is more like :date[web]
I found the default timezone is "+0000" in output log on my servers, How do I change the timezone?
example, change to: 02/Jun/2015:08:06:14 +0300
who know it? please help me, thanks~
Specifically I'd like to use morgan to write documents to a mongo db.
It would be cool if there was a way to add text next to a token without needing a space or needing to figure out how to rewrite the token so that it includes the text.
For ex:
app.use(logger(':method :status :url :response-time ms :res[content-length]'));
What if I want to remove the space between the ms and the :repsonse-time without rewritting the token? Is there a way to do this? Thanks.
Date format should be like 2014-10-21 10:49:02.00
currently it is Tue, 21 Oct 2014 10:49:02 GMT
currently there is no option for date format
exports.token('date', function(){
return new Date().toUTCString();
});
Not an issue but a question - I can get my logs out to Loggly using this code as a hypothetical. However I am struggling to figure out how to pass nice JSON-formatted messages to Loggly which I think will make the service easier for me to use. Does anyone have a good working example of how to use morgan to create nice JSON output (and pass to Loggly)?
if (app.get('env') === 'production') {
var loggly = require('loggly');
var client = loggly.createClient({
token: 'my-token',
subdomain: 'something',
tags: ['NodeJS'],
json: true
});
// Stream Express Logging to Loggly
app.use(logger('short', {
stream: {
write: function (message, encoding) {
client.log(message);
}
}
}));
}
something like this:
{
"IP_ADDRESS": "127.0.0.1",
"METHOD": "GET",
"URL": "/api/lastfm",
... (you get the idea)
}
wtf haha just saw this in a blog post, the names are super awkward, It would be almost better to just have express-logger
over something obscure like morgan, not a fan of that. Personally I think they'd all be better off as express-* or connect-* than bad names
Is there a way to change the response time from milliseconds to seconds ?
Im looking for a way to get the formatted string that morgan would otherwise send to the console, so I can pass it and an object representing the data to my logger. Currently I am just building the object with tokens['token'] and using that object to build up a string to output, but it is rather ugly and hard to match the format that morgan outputs on its own.
The example in the "use custom token formats" section is broken. The function in this section is missing the req
argument:
morgan.token('id', function () {
return req.id
})
It should be:
morgan.token('id', function (req) {
return req.id
})
When using in combination with e.g. compression-middleware, the logger doen't output response sizes by default, since a "content-length"-header isn't there...
Is there a proper way to use, or auto-fallback to something like req.socket.bytesWritten
, to get a better picture of response sizes?
Hi there,
I couldn't find any example on how to define colors to custom log tokens.
Is there a snippet somewhere I can use ?
Thanks
Hi
Is there a way to set up morgan to skip adding new line when using stream option?
I am combining morgan's output to winston and both are adding LF line feed which results in double new lines...I can strip out the newline when it comes from morgan but it would be faster if it wasn't there at all in this scenario...
Thoughts?
Amir
If you're running a HTTP and HTTPS server side by side, it would be nice to differentiate their requests in the log. As far as I can see this isn't possible at the moment. I will submit a PR.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.