@marblejs/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

$ yarn add @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

filter

<optional> HttpRequest -> boolean

Usage

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

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

const middlewares = [
  logger$(),
  ...
];

export const listener = httpListener({
  middlewares,
  effects: [],
});
  1. Customized logging behavior:

import { httpListener } from '@marblejs/core';
import { logger$ } from '@marblejs/middleware-logger';
import { isTestEnv } from './util';

const middlewares = [
  logger$({
    silent: isTestEnv(),
    filter: req => req.response.statusCode >= 400;
  }),
  ...
];

export const listener = httpListener({
  middlewares,
  effects: [],
});
  • silent - When true the logging is turned off (usually useful during testing),

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

Last updated