Skip to content

Commit 00cc1ea

Browse files
Uzlopakwolfy1339
authored andcommitted
fix: add createLogger to ensure that pino does not break (#744)
1 parent 99b1b30 commit 00cc1ea

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/index.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ const noop = () => {};
2222
const consoleWarn = console.warn.bind(console);
2323
const consoleError = console.error.bind(console);
2424

25+
/**
26+
* Creates a logger object for the Octokit instance.
27+
*
28+
* If a logger is provided, it will ensure that the logger has
29+
* `debug`, `info`, `warn`, and `error` methods.
30+
* If no logger is provided, it will create a default logger.
31+
*
32+
* Some Loggers like pino need that the this reference point
33+
* to the original object, so we cannot use `Object.assign` here.
34+
*/
35+
function createLogger(logger = {} as NonNullable<OctokitOptions["log"]>) {
36+
if (typeof logger.debug !== "function") {
37+
logger.debug = noop;
38+
}
39+
if (typeof logger.info !== "function") {
40+
logger.info = noop;
41+
}
42+
if (typeof logger.warn !== "function") {
43+
logger.warn = consoleWarn;
44+
}
45+
if (typeof logger.error !== "function") {
46+
logger.error = consoleError;
47+
}
48+
return logger as NonNullable<OctokitOptions["log"]>;
49+
}
50+
2551
const userAgentTrail = `octokit-core.js/${VERSION} ${getUserAgent()}`;
2652

2753
// Utility to omit a key and still keep dynamic properties
@@ -122,15 +148,7 @@ export class Octokit {
122148

123149
this.request = request.defaults(requestDefaults);
124150
this.graphql = withCustomRequest(this.request).defaults(requestDefaults);
125-
this.log = Object.assign(
126-
{
127-
debug: noop,
128-
info: noop,
129-
warn: consoleWarn,
130-
error: consoleError,
131-
},
132-
options.log,
133-
);
151+
this.log = createLogger(options.log);
134152
this.hook = hook;
135153

136154
// (1) If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance

0 commit comments

Comments
 (0)