From 3b0d314c0ad131d19f1697540c05aa97a6da0312 Mon Sep 17 00:00:00 2001 From: Jeff Mealo Date: Fri, 28 Jul 2023 05:57:07 -0400 Subject: [PATCH] Fix: #289 add documentation and typescript definitions for res.log (#290) --- README.md | 2 ++ index.d.ts | 2 ++ index.test-d.ts | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 7fd9fd0..aa78fc6 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,7 @@ const logger = require('pino-http')({ function handle (req, res) { logger(req, res) req.log.info('something else') + res.log.info('just in case you need access to logging when only the response is in scope') res.end('hello world') } @@ -300,6 +301,7 @@ http.createServer((req, res) => { res[logger.startTime] = Date.now() someImportantThingThatHasToBeFirst(req, res) logger(req, res) + res.log.info('log is available on both req and res'); res.end('hello world') }).listen(3000) ``` diff --git a/index.d.ts b/index.d.ts index 93ab974..57ae353 100644 --- a/index.d.ts +++ b/index.d.ts @@ -89,5 +89,7 @@ declare module "http" { interface OutgoingMessage { [startTime]: number; + log: pino.Logger; + allLogs: pino.Logger[]; } } diff --git a/index.test-d.ts b/index.test-d.ts index 0a51d6f..2c1ffd6 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -138,6 +138,8 @@ const stdSerializedResults: StdSerializedResults = { 123: {}, }, req: { + params: {}, + query: {}, id: canBeUndefined('id'), method: 'GET', url: 'http://0.0.0.0', @@ -159,5 +161,8 @@ const httpServerListener: RequestListener = (request, response) => { request.allLogs[0].info("Request Received"); // res[startTime] should be available response[startTime] = Date.now(); + // res.log and res.allLogs should be available + response.log.info("Logging works on response"); + request.allLogs[0].info("allLogs available on response"); response.end("Hello world"); };