Documentation forLoggly

Node Express.js and Morgan logging

Express.js

Express.js is one of the most popular frameworks for creating web applications with Node.js. If you choose to use it, there will come a time when you’ll want to add logging. One of the great things about Express.js and Node.js in general is that the setup and configuration is so simple.

Let’s assume that you have a very basic application and you want to log details about each request. In the code example below, you can see how to do it using a combination of Winston along with the express-winston logging extension. express-winston is a middleware library for Express.js which provides request and error logging in express.js applications.

var express = require('express');
var expressWinston = require('express-winston');
var winston = require('winston');
var app = module.exports = express();
var router = express.Router();

// Place the express-winston logger before the router.
app.use(expressWinston.logger({
  transports: [
    new winston.transports.Console({
      json: true,
      colorize: true
    })
  ]
}));

app.use(router);

// Place the express-winston errorLogger after the router.
app.use(expressWinston.errorLogger({
  transports: [
    new winston.transports.Console({
      json: true,
      colorize: true
    })
  ]
})); 

Sending Request Logs to Loggly

Request logs can easily be sent to Loggly, while continuing to log to the console. Simply add the Loggly transport. Your complete express.js application should look like the following:

var express = require('express');
var expressWinston = require('express-winston');
var winston = require('winston');
require('winston-loggly-bulk');

var app = module.exports = express();

app.use(expressWinston.logger({
  transports: [
    new winston.transports.Console({
      json: true,
      colorize: true
    }),
    new winston.transports.Loggly({
      subdomain: 'SUBDOMAIN',
      inputToken: 'TOKEN',
      json: true,
      tags: ["NodeJS-Express"]
    })
  ]
}));

app.get('/login', function (req, res) {
  res.send('Hi there..you have logged in')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
}) 

Replace:

  • SUBDOMAIN: enter the subdomain of your Loggly account
  • TOKEN: enter your customer token from the Source Setup page in Loggly

Save this file as app.js and then access the localhost URL on your web browser to generate a request to the server:

https://localhost:3000/login 

All that was required to set up logging was to require the necessary libraries, then configure a logger on the app, which, in this case, logs to the console. Now, when a request is made, in the console, you’ll see details written out about it, such as:

{
  "res": {
    "statusCode": 200
  },
  "req": {
    "url": "/login",
    "headers": {
      "host": "localhost:3000",
      "connection": "keep-alive",
      "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
      "upgrade-insecure-requests": "1",
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
      "accept-encoding": "gzip, deflate, br",
      "accept-language": "en-US,en;q=0.9",
      "cookie": "_ga=GA1.1.152534904.1515255925; __smToken=Z9sAgnI0y5w6cUXLMOswxVzQ",
      "if-none-match": "W/\"c-Lve95gjOVATpfV8EL5X4nxwjKHE\""
    },
    "method": "GET",
    "httpVersion": "1.1",
    "originalUrl": "/login",
    "query": {}
  },
  "responseTime": 5,
  "level": "info",
  "message": "HTTP GET /login"
} 

Verify Events

Go to the Search tab in Loggly and search for events with the NodeJS-Express tag over the last 20 minutes. It may take a few minutes to index the events. If it doesn’t work, refer to the troubleshooting section below.

tag:NodeJS-Express 

Request Logging with Morgan

Morgan is another HTTP request logger middleware for Node.js. It simplifies the process of logging requests to your application.

Using Morgan and Winston Together

Winston gives you a lot more flexibility with additional transports and also makes it easy to query your logs for analysis. Here is a code sample showing how to stream Morgan logs through Winston’s transports.

var express = require('express');
var expressWinston = require('express-winston');
var winston = require('winston');
require('winston-loggly-bulk');

var app = module.exports = express();

var logger = new winston.Logger({
  transports: [
    new winston.transports.Loggly({
      subdomain: 'SUBDOMAIN',
      inputToken: 'TOKEN',
      json: true,
      tags: ["Winston-Morgan"]
    }),
    new winston.transports.Console({
      level: 'debug',
      handleExceptions: true,
      json: false,
      colorize: true
    })
  ],
  exitOnError: false
}),
			
loggerstream = {
  write: function (message, encoding) {
    logger.info(message);
  }
};

app.use(require("morgan")("combined", { "stream": loggerstream }));

app.get('/logout', function (req, res) {
  res.send('Hi there..you have logged in')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
}) 

Replace:

  • SUBDOMAIN: enter the subdomain of your Loggly account
  • TOKEN: enter your customer token from the Source Setup page in Loggly

Save this file as app.js and then access the localhost URL on your web browser to generate a request to the server:

https://localhost:3000/logout 

Now, when a request is made, in the console, you’ll see details written out about it, such as:

info: ::1 - - [16/Feb/2018:12:37:44 +0000] "GET /logout HTTP/1.1" 200 34 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
timestamp=2018-02-16T12:37:44.421Z 

Verify Events

Go to the Search tab in Loggly and search for events with the Winston-Morgan tag over the last 20 minutes. It may take a few minutes to index the events. If it doesn’t work, refer to troubleshooting section below.

tag: Winston-Morgan 

Advanced Options

  • You can also use Winston’s File transporter to write your application logs into a log file. Please see the details here.

Express.js & Morgan Troubleshooting

If you don’t see any data show up in the verification step, check for these common problems.

  • Wait a few minutes in case indexing needs to catch up.
  • See our HTTP Troubleshooting Guide to verify HTTP events are being sent to Loggly.

Still Not Working?

  • Search or post your own questions on Express or Morgan logging, logging levels, or log messages in the community forum.

When the APM Integrated Experience is enabled, Loggly shares a common navigation and settings with the other integrated experiences' products. How you navigate Loggly and access its features may vary from these instructions. For more information, go to the APM Integrated Experience documentation.

The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.