Skip to content

Commit 78c6df8

Browse files
Uzlopakwolfy1339
authored andcommitted
fix: add createLogger to ensure that pino does not break (#744)
1 parent 38dd554 commit 78c6df8

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
@@ -23,6 +23,32 @@ const noop = () => {};
2323
const consoleWarn = console.warn.bind(console);
2424
const consoleError = console.error.bind(console);
2525

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

2854
export class Octokit {
@@ -117,15 +143,7 @@ export class Octokit {
117143

118144
this.request = request.defaults(requestDefaults);
119145
this.graphql = withCustomRequest(this.request).defaults(requestDefaults);
120-
this.log = Object.assign(
121-
{
122-
debug: noop,
123-
info: noop,
124-
warn: consoleWarn,
125-
error: consoleError,
126-
},
127-
options.log,
128-
);
146+
this.log = createLogger(options.log);
129147
this.hook = hook;
130148

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

0 commit comments

Comments
 (0)