@composable-software/commercetools-pino-middleware
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

commercetools-pino-middleware

npm npm

Overview

commercetools-pino-middleware is a library that provides a seamless integration of Pino logger with the commercetools SDK. It allows you to easily log SDK requests, responses, and other relevant information with Pino, a fast and minimalist Node.js logger. The middleware is designed to be flexible and can be set up with either an auto-generated Pino instance or a custom Pino logger with specific configurations.

Installation

You can install the library via npm:

npm install @composable-software/commercetools-pino-middleware

or with yarn

yarn add @composable-software/commercetools-pino-middleware

Usage

1. Using Auto-generated Pino Instance

If you prefer a hassle-free setup, you can let the middleware create and configure the Pino instance for you. Simply pass an empty object when setting up the middleware:

import { createPinoMiddleware } from '@composable-software/commercetools-pino-middleware';
import { ClientBuilder } from '@commercetools/sdk-client-v2';

/**
 * Middleware with automatic Pino factory
 */
const client = new ClientBuilder()
  .withMiddleware(createPinoMiddleware({}))
  .build();

In this method, the middleware will handle the instantiation and configuration of the Pino logger automatically.

2. Using a Custom Pino Instance

If you need a more customized Pino logger, you can pass your own Pino instance through options:

import pino from 'pino';
import { createPinoMiddleware } from '@composable-software/commercetools-pino-middleware';
import { ClientBuilder } from '@commercetools/sdk-client-v2';

/**
 * Custom Pino logger instance that can be passed to the middleware
 */
const logger = pino({
  name: 'custom-logger',
  level: 'info',
});

const options = {
  logger: logger,
};

const client = new ClientBuilder()
  .withMiddleware(createPinoMiddleware(options))
  .build();

In this case, the middleware will use the provided Pino instance to log the information, allowing you to have full control over the logger's configuration.

Logging Details

The commercetools-pino-middleware logs essential details related to SDK requests and responses, including:

  • Request method and URL
  • Request headers
  • Request body (if applicable)
  • Response status code
  • Response headers
  • Response body (if applicable)

All logs are output in a structured JSON format, which makes it easy to parse and analyze the logged data.

Examples

Here are some examples of how the middleware logs different scenarios:

  • Successful Request:
{"level":"info","time":1678376443924,"msg":"Request sent","method":"POST","url":"https://api.commercetools.com/products","headers":{"Authorization":"Bearer <access_token>","Content-Type":"application/json"},"body":{"typeId":"product","id":"abc123","version":5}}
{"level":"info","time":1678376453920,"msg":"Response received","status":201,"headers":{"x-request-id":"abc123","content-type":"application/json"},"body":{"id":"abc123","version":5}}
  • Failed Request:
{"level":"error","time":1678376463922,"msg":"Request failed","method":"GET","url":"https://api.commercetools.com/products/xyz789","headers":{"Authorization":"Bearer <access_token>"}}
{"level":"error","time":1678376473923,"msg":"Response received with error","status":404,"headers":{"x-request-id":"xyz789"},"body":{"statusCode":404,"message":"Not Found"}}

Contributing

We welcome contributions from the community! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on our GitHub repository.

License

This library is licensed under the MIT License. See the LICENSE file for more details.

Package Sidebar

Install

npm i @composable-software/commercetools-pino-middleware

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

8.73 kB

Total Files

11

Last publish

Collaborators

  • ikhvost
  • mrdck