Skip to content

Instantly share code, notes, and snippets.

@leommoore
Last active November 14, 2024 05:46
Show Gist options
  • Save leommoore/7524073 to your computer and use it in GitHub Desktop.
Save leommoore/7524073 to your computer and use it in GitHub Desktop.
Express - Logging

#Express - Logging The express.js node.js web application framework comes with a built-in logging module called logger which is the connect.js logger. It is really handy to enable and you can use it just like any other Express module. app.use(express.logger());

Without any configuration, the logger middleware will generate a detailed log using what is called the default format. The logger actually supports four predefined log formats: default, short ,tiny, and dev. Each of these predefined formats show various amounts of detail. You can specify one of them this way:

app.use(express.logger('dev'));

If you prefer, you can customize the precise details to be logged using the the following options to format the output of the logger:

Token Content
:req[header] The specific HTTP header of the request
:res[header] The specific HTTP header of the response
:http-version The HTTP version
:response-time How long it took to generate the response
:remote-addr The user agent's IP address
:date Date and time of request
:method The HTTP method used for making the request
:url The requested URL
:referrer The URL that referred the current URL
:user-agent The user-agent signature
:status The HTTP statusL

To specify the format just specify it like this:

app.use(express.logger({ 
  format: ':remote-addr :method :url' }
));

By default it will log everything to STDOUT but you can also configure it to log to a file like:

var http = require('http');
var express = require('express');
var fs = require('fs');
var app = express();

app.use(express.logger({
  format: 'dev', 
  stream: fs.createWriteStream('app.log', {'flags': 'w'})
}));
@hamx0r
Copy link

hamx0r commented Sep 13, 2017

This all still seems valid, but logger now needs to be installed separately and has been renamed morgan

@myxibrium
Copy link

yeah, that's really stupid and confusing. The lack of good logging support out of the box in express is really stupid. There is no single use-case for zero logging support period. Plus Morgan only supports 3 datetime formats, in only UTC, and doesn't allow custom formats.

@khuzema786
Copy link

yeah, that's really stupid and confusing. The lack of good logging support out of the box in express is really stupid. There is no single use-case for zero logging support period. Plus Morgan only supports 3 datetime formats, in only UTC, and doesn't allow custom formats.

Khud banalo bhai tum

@Devendra0110
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment