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 { loggerWithOpts$ } from '@marblejs/middleware-logger';

From version v1.2 the logger$ entry point is marked as deprecated. Use loggerWithOpts$ instead. From the version v2.x the old entry point will be swapped with newer implementation.

Type declaration

loggerWithOpts$ :: (LoggerOptions) -> Middleware

Parameters

LoggerOptions

Usage

  1. Default behaviour. Log every response to process.stdout (console.log):

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

export const logger$ = loggerWithOpts$();

2. Customized logging behaviour:

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

export const logger$ = loggerWithOpts$({
  silent: isTestEnv(),
  stream: createWriteStream(PATH, { flags: 'a' });;
  filter: req => req.status >= 400;
});
  • 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 SOME_PATH variable,

  • filter - Function to determine if logging is skipped. For example we can log only HTTP status codes above 400).

Last updated