npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@composable-software/commercetools-pino-middleware

v1.0.0

Published

Pino logger for commercetools SDK

Downloads

5

Readme

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.