Skip to content

Express middleware

Installation

npm i leekslazylogger-express

Usage

// set up logger with options
const ExpressLogger = require('leekslazylogger-express');
const log = new ExpressLogger({
    name: 'My express server',
});

// require express
const express = require('express');
const app = express();

// use logger middleware
app.use(log.express());

...
// other middleware and router
...

// start server
app.listen(8080);

The logger middleware must be one of the first middleware to be added, before the routing.

Screenshot

image

Options

You can pass options to customise it:

const ExpressLogger = require('leekslazylogger-express');
const log = new ExpressLogger({
    name: 'My express server',
    levels: {
        http: {
            title: 'info',
            prefix: 'http'
        }
    }
});

app.use(log.express({
    level: 'http',
    format: '{method} {route} {status}'
}));

format

The default format is: {status-colour}{status}&r {method} &7{path} {time-colour}({time})

The string can include colour codes.

Must be a string, or a function (takes the req object) which returns a string.

Using a function allows use of other data such as the request's IP:

const requestIp = require('request-ip');

app.use(requestIp.mw());
app.use(log.express({
    level: 'http',
    format: req => `${req.clientIp} {method} {route} {status}`
}));

Placeholders

The available placeholders for setting your own format are:

  • {method}: GET/POST etc
  • {protocol}: HTTP or HTTPS
  • {route}: express route name (eg: /users/:id)
  • {path}: Full path (no query)
  • {status-colour} / {status-color}: green/orange/blue/red colour code based on status code (to prefix status)
  • {status}: status code (200, 301, 404 etc)
  • {time-colour} / {time-color}: light green/yellow/red colour code based on time (to prefix time)
  • {time}: time in ms for request to be completed

level

The logger level (function) to use. The default level is info (log.info()).


Last update: May 12, 2021