middleware-logger

HTTP request logger middleware for Marble.js

Simple middleware for request logging inside your console. It displays the outgoing request events using the following format:

{HTTP_METHOD} {PATH} {HTTP_STATUS} {TIME}
POST /api/v1/user 200 1ms

Installation

$ npm i @marblejs/middleware-logger

Requires @marblejs/core to be installed.

Importing

import { logger$ } from '@marblejs/middleware-logger';

Type declaration

logger$ :: LoggerOptions -> HttpMiddlewareEffect

Parameters

parameter

definition

options

<optional> LoggerOptions

LoggerOptions

parameter

definition

silent

<optional> boolean

stream

<optional> WriteStream

filter

<optional> (HttpResponse, HttpRequest) => boolean

Usage

  1. Default behaviour. Log every response to process.stdout:

logger.middleware.ts
import { logger$ } from '@marblejs/middleware-logger';
const middlewares = [
logger$(),
...
];
export const app = httpListener({ middlewares, effects: [] });

2. Customized logging behaviour:

logger.middleware.ts
import { logger$ } from '@marblejs/middleware-logger';
import { isTestEnv } from './util';
const middlewares = [
logger$({
silent: isTestEnv(),
stream: createWriteStream(PATH, { flags: 'a' });;
filter: (res, req) => res.status >= 400;
}),
...
];
export const app = httpListener({ middlewares, effects: [] });
  • silent - When true the logging is turned off (usually useful during testing),

  • stream - Output stream for writing log messages, defaults to process.stdout. In the example above every response will be written to file pointed by provided PATH variable,

  • filter - Filter outgoing responses or incoming requests based on given predicate. For example we can log only HTTP status codes above 400.