Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (69 sloc) 2.17 KB
description
HTTP request logger middleware for Marble.js

middleware-logger

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:

{% code-tabs %} {% code-tabs-item title="logger.middleware.ts" %}

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

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

export const app = httpListener({ middlewares, effects: [] });

{% endcode-tabs-item %} {% endcode-tabs %}

  1. Customized logging behaviour:

{% code-tabs %} {% code-tabs-item title="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: [] });

{% endcode-tabs-item %} {% endcode-tabs %}

  • 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.
You can’t perform that action at this time.