Skip to content

Commit

Permalink
Improve TS types (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Aug 27, 2021
1 parent 3ec9946 commit 3937987
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 66 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@
},
"homepage": "http://getpino.io",
"devDependencies": {
"@types/node": "^16.4.10",
"@types/node": "^16.7.2",
"airtap": "4.0.3",
"benchmark": "^2.1.4",
"bole": "^4.0.0",
"bunyan": "^1.8.14",
"docsify-cli": "^4.4.1",
"eslint": "^7.17.0",
"eslint-config-standard": "^16.0.2",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
Expand All @@ -92,9 +92,9 @@
"tap": "^15.0.1",
"tape": "^5.0.0",
"through2": "^4.0.0",
"ts-node": "^10.0.0",
"ts-node": "^10.2.1",
"tsd": "^0.17.0",
"typescript": "^4.3.5",
"typescript": "^4.4.2",
"winston": "^3.3.3"
},
"dependencies": {
Expand Down
116 changes: 55 additions & 61 deletions pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ declare namespace P {
/**
* Holds the current log format version (as output in the v property of each log record).
*/
const LOG_VERSION: number;
const levels: LevelMapping;
const symbols: {
readonly setLevelSym: unique symbol;
Expand Down Expand Up @@ -728,24 +727,9 @@ declare namespace P {
};
}

type Logger = BaseLogger & { [key: string]: LogFn };

interface BaseLogger extends EventEmitter {
/**
* Exposes the current version of Pino.
*/
readonly pino: string;
/**
* Holds the current log format version (as output in the v property of each log record).
*/
readonly LOG_VERSION: number;
/**
* Exposes the Pino package version. Also available on the exported pino function.
*/
readonly version: string;

levels: LevelMapping;
type Logger = BaseLogger & LoggerExtras & Record<string, any>;

interface BaseLogger {
/**
* Set this property to the desired logging level. In order of priority, available levels are:
*
Expand All @@ -762,49 +746,6 @@ declare namespace P {
* You can pass `'silent'` to disable logging.
*/
level: LevelWithSilent | string;
/**
* Outputs the level as a string instead of integer.
*/
useLevelLabels: boolean;
/**
* Define additional logging levels.
*/
customLevels: { [key: string]: number };
/**
* Use only defined `customLevels` and omit Pino's levels.
*/
useOnlyCustomLevels: boolean;
/**
* Returns the integer value for the logger instance's logging level.
*/
levelVal: number;

/**
* Registers a listener function that is triggered when the level is changed.
* Note: When browserified, this functionality will only be available if the `events` module has been required elsewhere
* (e.g. if you're using streams in the browser). This allows for a trade-off between bundle size and functionality.
*
* @param event: only ever fires the `'level-change'` event
* @param listener: The listener is passed four arguments: `levelLabel`, `levelValue`, `previousLevelLabel`, `previousLevelValue`.
*/
on(event: "level-change", listener: LevelChangeEventListener): this;
addListener(event: "level-change", listener: LevelChangeEventListener): this;
once(event: "level-change", listener: LevelChangeEventListener): this;
prependListener(event: "level-change", listener: LevelChangeEventListener): this;
prependOnceListener(event: "level-change", listener: LevelChangeEventListener): this;
removeListener(event: "level-change", listener: LevelChangeEventListener): this;

/**
* Creates a child logger, setting all key-value pairs in `bindings` as properties in the log lines. All serializers will be applied to the given pair.
* Child loggers use the same output stream as the parent and inherit the current log level of the parent at the time they are spawned.
* From v2.x.x the log level of a child is mutable (whereas in v1.x.x it was immutable), and can be set independently of the parent.
* If a `level` property is present in the object passed to `child` it will override the child logger level.
*
* @param bindings: an object of key-value pairs to include in log lines as properties.
* @param options: an options object that will override child logger inherited options.
* @returns a child logger instance.
*/
child(bindings: Bindings, options?: ChildLoggerOptions): Logger;

/**
* Log at `'fatal'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line.
Expand Down Expand Up @@ -870,6 +811,59 @@ declare namespace P {
* Noop function.
*/
silent: LogFn;
}

interface LoggerExtras extends EventEmitter {
/**
* Exposes the Pino package version. Also available on the exported pino function.
*/
readonly version: string;

levels: LevelMapping;

/**
* Outputs the level as a string instead of integer.
*/
useLevelLabels: boolean;
/**
* Define additional logging levels.
*/
customLevels: { [key: string]: number };
/**
* Use only defined `customLevels` and omit Pino's levels.
*/
useOnlyCustomLevels: boolean;
/**
* Returns the integer value for the logger instance's logging level.
*/
levelVal: number;

/**
* Creates a child logger, setting all key-value pairs in `bindings` as properties in the log lines. All serializers will be applied to the given pair.
* Child loggers use the same output stream as the parent and inherit the current log level of the parent at the time they are spawned.
* From v2.x.x the log level of a child is mutable (whereas in v1.x.x it was immutable), and can be set independently of the parent.
* If a `level` property is present in the object passed to `child` it will override the child logger level.
*
* @param bindings: an object of key-value pairs to include in log lines as properties.
* @param options: an options object that will override child logger inherited options.
* @returns a child logger instance.
*/
child(bindings: Bindings, options?: ChildLoggerOptions): Logger;

/**
* Registers a listener function that is triggered when the level is changed.
* Note: When browserified, this functionality will only be available if the `events` module has been required elsewhere
* (e.g. if you're using streams in the browser). This allows for a trade-off between bundle size and functionality.
*
* @param event: only ever fires the `'level-change'` event
* @param listener: The listener is passed four arguments: `levelLabel`, `levelValue`, `previousLevelLabel`, `previousLevelValue`.
*/
on(event: "level-change", listener: LevelChangeEventListener): this;
addListener(event: "level-change", listener: LevelChangeEventListener): this;
once(event: "level-change", listener: LevelChangeEventListener): this;
prependListener(event: "level-change", listener: LevelChangeEventListener): this;
prependOnceListener(event: "level-change", listener: LevelChangeEventListener): this;
removeListener(event: "level-change", listener: LevelChangeEventListener): this;

/**
* A utility method for determining if a given log level will write to the destination.
Expand Down
7 changes: 6 additions & 1 deletion test/types/pino.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import P, { pino } from "../../";
import { IncomingMessage, ServerResponse } from "http";
import { Socket } from "net";
import Logger = P.Logger;

const log = pino();
const info = log.info;
Expand Down Expand Up @@ -106,7 +107,7 @@ childRedacted.info({
msg: "logged with redacted properties",
path: "Not shown",
});
const childAnotherRedacted = pino().child({}, {
const childAnotherRedacted = pino().child({}, {
redact: {
paths: ["anotherPath"],
censor: "Not the log you\re looking for",
Expand Down Expand Up @@ -267,3 +268,7 @@ const logLine: pino.LogDescriptor = {
time: new Date().getTime(),
aCustomProperty: true,
};

interface CustomLogger extends pino.Logger {
customMethod(msg: string, ...args: unknown[]): void;
}

0 comments on commit 3937987

Please sign in to comment.