|
1 | 1 | # commercetools-pino-middleware |
| 2 | + |
| 3 | +  |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`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. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +You can install the library via npm: |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install @composable-commerce/commercetools-pino-middleware |
| 15 | +``` |
| 16 | +or with yarn |
| 17 | +```bash |
| 18 | +yarn add @composable-commerce/commercetools-pino-middleware |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### 1. Using Auto-generated Pino Instance |
| 24 | + |
| 25 | +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: |
| 26 | + |
| 27 | +```javascript |
| 28 | +import { createPinoMiddleware } from 'commercetools-pino-middleware'; |
| 29 | +import { ClientBuilder } from '@commercetools/sdk-client-v2'; |
| 30 | + |
| 31 | +/** |
| 32 | + * Middleware with automatic Pino factory |
| 33 | + */ |
| 34 | +const client = new ClientBuilder() |
| 35 | + .withMiddleware(createPinoMiddleware({})) |
| 36 | + .build(); |
| 37 | +``` |
| 38 | + |
| 39 | +In this method, the middleware will handle the instantiation and configuration of the Pino logger automatically. |
| 40 | + |
| 41 | +### 2. Using a Custom Pino Instance |
| 42 | + |
| 43 | +If you need a more customized Pino logger, you can pass your own Pino instance through options: |
| 44 | + |
| 45 | +```javascript |
| 46 | +import pino from 'pino'; |
| 47 | +import { createPinoMiddleware } from 'commercetools-pino-middleware'; |
| 48 | +import { ClientBuilder } from '@commercetools/sdk-client-v2'; |
| 49 | + |
| 50 | +/** |
| 51 | + * Custom Pino logger instance that can be passed to the middleware |
| 52 | + */ |
| 53 | +const logger = pino({ |
| 54 | + name: 'custom-logger', |
| 55 | + level: 'info', |
| 56 | +}); |
| 57 | + |
| 58 | +const options = { |
| 59 | + logger: logger, |
| 60 | +}; |
| 61 | + |
| 62 | +const client = new ClientBuilder() |
| 63 | + .withMiddleware(createPinoMiddleware(options)) |
| 64 | + .build(); |
| 65 | +``` |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +## Logging Details |
| 70 | + |
| 71 | +The `commercetools-pino-middleware` logs essential details related to SDK requests and responses, including: |
| 72 | + |
| 73 | +- Request method and URL |
| 74 | +- Request headers |
| 75 | +- Request body (if applicable) |
| 76 | +- Response status code |
| 77 | +- Response headers |
| 78 | +- Response body (if applicable) |
| 79 | + |
| 80 | +All logs are output in a structured JSON format, which makes it easy to parse and analyze the logged data. |
| 81 | + |
| 82 | +## Examples |
| 83 | + |
| 84 | +Here are some examples of how the middleware logs different scenarios: |
| 85 | + |
| 86 | +- **Successful Request:** |
| 87 | + |
| 88 | +```json |
| 89 | +{"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}} |
| 90 | +{"level":"info","time":1678376453920,"msg":"Response received","status":201,"headers":{"x-request-id":"abc123","content-type":"application/json"},"body":{"id":"abc123","version":5}} |
| 91 | +``` |
| 92 | + |
| 93 | +- **Failed Request:** |
| 94 | + |
| 95 | +```json |
| 96 | +{"level":"error","time":1678376463922,"msg":"Request failed","method":"GET","url":"https://api.commercetools.com/products/xyz789","headers":{"Authorization":"Bearer <access_token>"}} |
| 97 | +{"level":"error","time":1678376473923,"msg":"Response received with error","status":404,"headers":{"x-request-id":"xyz789"},"body":{"statusCode":404,"message":"Not Found"}} |
| 98 | +``` |
| 99 | + |
| 100 | +## Contributing |
| 101 | + |
| 102 | +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](https://github.com/your-github-account/commercetools-pino-middleware). |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +This library is licensed under the [MIT License](https://opensource.org/licenses/MIT). See the [LICENSE](./LICENSE) file for more details. |
0 commit comments