{"library":"pino-http-send","title":"Pino HTTP Log Transport","description":"pino-http-send is a basic handler for Pino logs that facilitates sending batches of structured log data to a desired HTTP or HTTPS endpoint. Currently at version 0.4.2, it is pre-v1, meaning minor version changes may introduce breaking changes. The library supports configurable HTTP methods (POST, PUT, PATCH, GET), two body types (JSON array wrapped in a 'logs' object or newline-delimited JSON), and includes basic authentication and retry mechanisms for failed sends. It can be used either as a command-line interface tool, piping `pino` output directly, or programmatically via its `createWriteStream` API, which acts as a Pino destination. Its key differentiators include its simplicity in setting up a direct HTTP log sink, batching capabilities to optimize network requests, and built-in retry logic, making it a robust, low-overhead option for forwarding Pino logs.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install pino-http-send"],"cli":{"name":"pino-http-send","version":null}},"imports":["import { createWriteStream } from 'pino-http-send';","import type { PinoHttpSendOptions } from 'pino-http-send';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createWriteStream } from 'pino-http-send';\nimport pino from 'pino';\nimport http from 'http';\n\n// 1. Setup a dummy HTTP server to receive logs (your actual log ingestion endpoint)\nconst server = http.createServer((req, res) => {\n  if (req.url === '/logs' && req.method === 'POST') {\n    let body = '';\n    req.on('data', (chunk) => { body += chunk.toString(); });\n    req.on('end', () => {\n      try {\n        console.log(`\\n--- Received Batch (${req.headers['content-type']}) ---\\n`);\n        if (req.headers['content-type']?.includes('application/json')) {\n          console.log(JSON.parse(body).logs); // 'json' bodyType wraps logs in { logs: [...] }\n        } else {\n          console.log(body); // 'ndjson' is raw new-line delimited JSON\n        }\n        console.log(`--- End Batch ---\\n`);\n        res.writeHead(200, { 'Content-Type': 'application/json' });\n        res.end(JSON.stringify({ status: 'ok', receivedLogsCount: body.split('\\n').filter(Boolean).length }));\n      } catch (e) {\n        console.error('Error parsing received logs:', e);\n        res.writeHead(400).end('Bad Request');\n      }\n    });\n  } else {\n    res.writeHead(404).end('Not Found');\n  }\n});\n\nserver.listen(3000, () => {\n  console.log('Dummy log server listening on http://localhost:3000');\n  console.log('Sending logs via pino-http-send...');\n\n  // 2. Configure pino-http-send as a Pino destination\n  const pinoSendStream = createWriteStream({\n    url: 'http://localhost:3000/logs',\n    method: 'POST',\n    bodyType: 'json', // or 'ndjson'\n    batchSize: 2,      // Send every 2 logs\n    timeout: 1000,     // or flush after 1 second if batch not full\n    log: true,         // Enable internal logging for pino-http-send itself\n    headers: { 'X-Custom-Header': 'pino-test' }\n  });\n\n  // 3. Create a Pino logger instance using the pino-http-send stream\n  const logger = pino(pinoSendStream);\n\n  let count = 0;\n  const interval = setInterval(() => {\n    logger.info({ id: ++count, service: 'my-app', event: 'data_processed', timestamp: new Date().toISOString() });\n    if (count >= 5) {\n      clearInterval(interval);\n      logger.flush(); // Ensure any buffered logs are sent\n      setTimeout(() => {\n        server.close(() => console.log('Dummy server closed. Exiting.'));\n      }, 2000); // Give time for the last batch to be sent\n    }\n  }, 500);\n});","lang":"typescript","description":"Demonstrates programmatic usage of `pino-http-send` by creating a dummy HTTP server to receive logs and configuring Pino to send logs to it via `createWriteStream`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}