From 64305fc7f9e77c31a3fdf7b3bb6d493fe6e2a156 Mon Sep 17 00:00:00 2001 From: Matt Jeanes Date: Tue, 22 Oct 2019 16:26:12 -0400 Subject: [PATCH] [@types/hapi-pino] fix/improve types, improve tests (#39219) * [@types/hapi-pino] fix/improve types, improve tests * [@types/hapi-pino] Update to hapi-pino to 6.3 --- types/hapi-pino/hapi-pino-tests.ts | 106 ++++++++++++++++++++--------- types/hapi-pino/index.d.ts | 29 +++++--- 2 files changed, 93 insertions(+), 42 deletions(-) diff --git a/types/hapi-pino/hapi-pino-tests.ts b/types/hapi-pino/hapi-pino-tests.ts index 7faa159273..385ea2079f 100644 --- a/types/hapi-pino/hapi-pino-tests.ts +++ b/types/hapi-pino/hapi-pino-tests.ts @@ -1,4 +1,4 @@ -import { Server } from '@hapi/hapi'; +import { Request, Server } from '@hapi/hapi'; import * as pino from 'pino'; import * as HapiPino from 'hapi-pino'; @@ -6,39 +6,79 @@ const pinoLogger = pino(); const server = new Server(); -server.register({ - plugin: HapiPino, - options: { - logPayload: false, - logRouteTags: false, - stream: process.stdout, - prettyPrint: process.env.NODE_ENV !== 'PRODUCTION', - levelTags: { - trace: 'trace', - debug: 'debug', - info: 'info', - warn: 'warn', - error: 'error' +function example1() { + server + .register({ + plugin: HapiPino, + options: { + logPayload: false, + logRouteTags: false, + stream: process.stdout, + prettyPrint: process.env.NODE_ENV !== 'PRODUCTION', + + tags: { + trace: 'trace', + debug: 'debug', + info: 'info', + warn: 'warn', + error: 'error', + fatal: 'fatal', + }, + allTags: 'info', + serializers: { + req: (req: any) => console.log(req), + }, + instance: pinoLogger, + logEvents: false, + mergeHapiLogData: false, + ignorePaths: ['/testRoute'], + level: 'debug', + redact: ['test.property'], + }, + }) + .then(() => { + server.logger().debug('using logger object directly'); + + server.route({ + method: 'GET', + path: '/', + handler: (request, h) => { + request.logger.debug('using logger directly'); + }, + }); + }); +} + +function example2() { + server.register({ + plugin: HapiPino, + options: { + redact: { + paths: ['test.property', 'another.property'], + remove: true, + }, + logRequestStart: true, + prettyPrint: { + levelFirst: true, + colorize: true, + translateTime: 'SYS:yyyy-mm-dd HH:MM:ss.l', + ignore: 'hostname,pid', + }, + getChildBindings: (req: Request) => ({ + 'x-request-id': req.headers['x-request-id'], + }), }, - allTags: 'info', - serializers: { - req: (req: any) => console.log(req) - }, - instance: pinoLogger, - logEvents: false, - mergeHapiLogData: false, + }); +} + +function example3() { + HapiPino.register(server, { ignorePaths: ['/testRoute'], level: 'debug', - redact: ['test.property'] - } -}).then(() => { - server.logger().debug('using logger object directly'); - - server.route({ - method: 'GET', - path: '/', - handler: (request, h) => { - request.logger.debug('using logger directly'); - } + tags: { + trace: 'trace', + debug: 'debug', + }, + redact: ['test.property'], }); -}); +} diff --git a/types/hapi-pino/index.d.ts b/types/hapi-pino/index.d.ts index 7afbcafed4..388a38b5d7 100644 --- a/types/hapi-pino/index.d.ts +++ b/types/hapi-pino/index.d.ts @@ -1,7 +1,8 @@ -// Type definitions for hapi-pino 6.0 +// Type definitions for hapi-pino 6.3 // Project: https://github.com/pinojs/hapi-pino#readme // Definitions by: Rodrigo Saboya // Todd Bealmear +// Matt Jeanes // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -9,7 +10,7 @@ import * as pino from 'pino'; -import { Plugin } from '@hapi/hapi'; +import { Plugin, Request } from '@hapi/hapi'; declare module '@hapi/hapi' { interface Server { @@ -22,22 +23,32 @@ declare module '@hapi/hapi' { } declare namespace HapiPino { - type LogLevels = 'trace' | 'debug' | 'info' | 'warn' | 'error'; + interface Serializers { + [key: string]: pino.SerializerFn; + } interface Options { logPayload?: boolean; logRouteTags?: boolean; + logRequestStart?: boolean; stream?: NodeJS.WriteStream; - prettyPrint?: boolean; - levelTags?: { [key in LogLevels]: string }; - allTags?: LogLevels; - serializers?: { [key: string]: (param: any) => void}; + prettyPrint?: boolean | pino.PrettyOptions; + tags?: { [key in pino.Level]?: string }; + allTags?: pino.Level; + serializers?: Serializers; + getChildBindings?: ( + req: Request, + ) => { + level?: pino.Level | string; + serializers?: Serializers; + [key: string]: any; + }; instance?: pino.Logger; logEvents?: string[] | false | null; mergeHapiLogData?: boolean; ignorePaths?: string[]; - level?: LogLevels; - redact?: string[]; + level?: pino.Level; + redact?: string[] | pino.redactOptions; } }