Skip to content

Commit

Permalink
feat(#1595): improve stream entry types (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
Puppo authored Aug 10, 2023
1 parent da75c01 commit 91a3505
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
22 changes: 11 additions & 11 deletions pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,18 @@ declare namespace pino {

type DestinationStreamWithMetadata = DestinationStream & ({ [symbols.needsMetadataGsym]?: false } | DestinationStreamHasMetadata);

interface StreamEntry {
interface StreamEntry<TLevel = Level> {
stream: DestinationStream
level?: Level
level?: TLevel
}

interface MultiStreamRes {
interface MultiStreamRes<TOriginLevel = Level> {
write: (data: any) => void,
add: (dest: StreamEntry | DestinationStream) => MultiStreamRes,
add: <TLevel = Level>(dest: StreamEntry<TLevel> | DestinationStream) => MultiStreamRes<TOriginLevel & TLevel>,
flushSync: () => void,
minLevel: number,
streams: StreamEntry[],
clone(level: Level): MultiStreamRes,
streams: StreamEntry<TOriginLevel>[],
clone<TLevel = Level>(level: TLevel): MultiStreamRes<TLevel>,
}

interface LevelMapping {
Expand Down Expand Up @@ -775,10 +775,10 @@ declare namespace pino {
options: TransportSingleOptions<TransportOptions> | TransportMultiOptions<TransportOptions> | TransportPipelineOptions<TransportOptions>
): ThreadStream

export function multistream(
streamsArray: (DestinationStream | StreamEntry)[] | DestinationStream | StreamEntry,
export function multistream<TLevel = Level>(
streamsArray: (DestinationStream | StreamEntry<TLevel>)[] | DestinationStream | StreamEntry<TLevel>,
opts?: MultiStreamOptions
): MultiStreamRes
): MultiStreamRes<TLevel>
}

//// Callable default export
Expand Down Expand Up @@ -833,8 +833,8 @@ export interface LogEvent extends pino.LogEvent {}
export interface LogFn extends pino.LogFn {}
export interface LoggerOptions extends pino.LoggerOptions {}
export interface MultiStreamOptions extends pino.MultiStreamOptions {}
export interface MultiStreamRes extends pino.MultiStreamRes {}
export interface StreamEntry extends pino.StreamEntry {}
export interface MultiStreamRes<TLevel = Level> extends pino.MultiStreamRes<TLevel> {}
export interface StreamEntry<TLevel = Level> extends pino.StreamEntry<TLevel> {}
export interface TransportBaseOptions extends pino.TransportBaseOptions {}
export interface TransportMultiOptions extends pino.TransportMultiOptions {}
export interface TransportPipelineOptions extends pino.TransportPipelineOptions {}
Expand Down
22 changes: 12 additions & 10 deletions test/types/pino-multistream.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ import { expectType } from 'tsd'

import { createWriteStream } from 'fs'

import pino from '../../pino'
import { multistream } from "../../pino";
import pino, { multistream } from '../../pino'

const streams = [
{ stream: process.stdout },
{ stream: createWriteStream('') },
{ level: 'error' as const, stream: process.stderr },
{ level: 'fatal' as const, stream: createWriteStream('') }
{ level: 'fatal' as const, stream: process.stderr },
]

expectType<pino.MultiStreamRes>(pino.multistream(process.stdout))
expectType<pino.MultiStreamRes>(pino.multistream([createWriteStream('')]))
expectType<pino.MultiStreamRes>(pino.multistream({ level: 'error' as const, stream: process.stderr }))
expectType<pino.MultiStreamRes>(pino.multistream([{ level: 'fatal' as const, stream: createWriteStream('') }]))
expectType<pino.MultiStreamRes<'error'>>(pino.multistream({ level: 'error' as const, stream: process.stderr }))
expectType<pino.MultiStreamRes<'fatal'>>(pino.multistream([{ level: 'fatal' as const, stream: createWriteStream('') }]))

expectType<pino.MultiStreamRes<'error' | 'fatal'>>(pino.multistream(streams))
expectType<pino.MultiStreamRes<'error' | 'fatal'>>(pino.multistream(streams, {}))
expectType<pino.MultiStreamRes<'error' | 'fatal'>>(pino.multistream(streams, { levels: { 'info': 30 } }))
expectType<pino.MultiStreamRes<'error' | 'fatal'>>(pino.multistream(streams, { dedupe: true }))
expectType<pino.MultiStreamRes<'error' | 'fatal'>>(pino.multistream(streams[0]).add(streams[1]))
expectType<pino.MultiStreamRes<'error' | 'fatal'>>(multistream(streams))
expectType<pino.MultiStreamRes<'error'>>(multistream(streams).clone('error'))

expectType<pino.MultiStreamRes>(pino.multistream(streams))
expectType<pino.MultiStreamRes>(pino.multistream(streams, {}))
expectType<pino.MultiStreamRes>(pino.multistream(streams, { levels: { 'info': 30 } }))
expectType<pino.MultiStreamRes>(pino.multistream(streams, { dedupe: true }))
expectType<pino.MultiStreamRes>(pino.multistream(streams[0]).add(streams[1]))

expectType<pino.MultiStreamRes>(multistream(process.stdout));
38 changes: 37 additions & 1 deletion test/types/pino.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { pino } from '../../pino'
import { StreamEntry, pino } from '../../pino'
import { join } from 'path'
import { tmpdir } from 'os'
import pinoPretty from 'pino-pretty'

const destination = join(
tmpdir(),
Expand Down Expand Up @@ -40,3 +41,38 @@ const transports = pino.transport({targets: [
]})
const loggerMulti = pino(transports)
loggerMulti.info('test2')

// custom levels

const customLevels = {
debug : 1,
info : 2,
network : 3,
error : 4,
};

type CustomLevels = keyof typeof customLevels;

const pinoOpts = {
level: 'debug',
useOnlyCustomLevels: true,
customLevels: customLevels,
};

const multistreamOpts = {
dedupe: true,
levels: customLevels
};

const streams: StreamEntry<CustomLevels>[] = [
{ level : 'debug', stream : pinoPretty() },
{ level : 'info', stream : pinoPretty() },
{ level : 'network', stream : pinoPretty() },
{ level : 'error', stream : pinoPretty() },
];

const loggerCustomLevel = pino(pinoOpts, pino.multistream(streams, multistreamOpts));
loggerCustomLevel.debug('test3')
loggerCustomLevel.info('test4')
loggerCustomLevel.error('test5')
loggerCustomLevel.network('test6')

0 comments on commit 91a3505

Please sign in to comment.