Code Monkey home page Code Monkey logo

morgan's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

morgan's Issues

The ANSI Grey being used conflicts with many Themes

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.

Example in "use custom token formats" section is broken

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
})

Response time should be a number, not a string

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.

Reopen the log files, Error: write after end

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]

Examples outdated ?

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
}));

Ignore socket.io poll

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!

Passing skip function not working

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;
            }
        }
    )
);

add text next to a token without needing a space

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.

Runtime change logging level

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.

Streaming JSON logs to Loggly

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)
}

how to log response message?

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.

Ordering of log lines

Often the log lines get out of order, or one request will be fully coloured green like the 200 request colour. I'm guessing this is due to the async nature of the console, just alerting you to it.
2015_04_20_11_02_28_c_windows_system32_cmd exe_server_remote bat_

Add protocol token

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.

Heroku: Cannot find module 'debug'

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

json format

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?

colo(u)rs?

Is there an option to have the colour output like it was in express 3.0?

Is there a way to get the formatted output string inside the format function?

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.

More response-sizes

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?

Feature request: log both before & after the request

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?

Syslog

Is it possible to config morgan to write to syslog via udp?

Format bytes using module `bytes`

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.

How do I log output to file?

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?

Why UTC string?

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();

Stop express polluting the logs

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.

Does 'response-time' include time to send response to client?

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?

name

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

Custom tokens format replaces 0 by '-'

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 = '-' }

Option to skip new line when writing to stream

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

how do I change the default timezone?

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~

JSON log format

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
}

How to change formatting?

I want change formatting in this logger ('dev') in Node.js + Express.js environment.

common usage:

    var morgan= require('morgan');
    // ...
    app.use(morgan('dev'));

result:
morgan logger - dev (default)

I need to change the formatting that I achieved the desired result:

morgan logger - dev (better formatting)

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.

Export compile function to be used by others

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.