@@ -23,6 +23,32 @@ const noop = () => {};
23
23
const consoleWarn = console . warn . bind ( console ) ;
24
24
const consoleError = console . error . bind ( console ) ;
25
25
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
+
26
52
const userAgentTrail = `octokit-core.js/${ VERSION } ${ getUserAgent ( ) } ` ;
27
53
28
54
export class Octokit {
@@ -117,15 +143,7 @@ export class Octokit {
117
143
118
144
this . request = request . defaults ( requestDefaults ) ;
119
145
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 ) ;
129
147
this . hook = hook ;
130
148
131
149
// (1) If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
0 commit comments