Skip to content

Commit

Permalink
fix: bind log (#1939)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Jan 18, 2024
1 parent 5e302ce commit e25f1ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/helpers/get-log.ts
Expand Up @@ -17,6 +17,7 @@
import { pino } from "pino";
import type { Logger, LoggerOptions } from "pino";
import { getTransformStream, type Options, type LogLevel } from "@probot/pino";
import { rebindLog } from "./rebind-log";

export type GetLogOptions = {
level?: LogLevel;
Expand All @@ -33,7 +34,6 @@ export function getLog(options: GetLogOptions = {}): Logger {
};
const transform = getTransformStream(getTransformStreamOptions);
transform.pipe(pino.destination(1));
const log = pino(pinoOptions, transform);

return log;
return rebindLog(pino(pinoOptions, transform));
}
17 changes: 17 additions & 0 deletions src/helpers/rebind-log.ts
@@ -0,0 +1,17 @@
import type { Logger } from "pino";

const kIsBound = Symbol("is-bound");

export function rebindLog(log: Logger): Logger {
// @ts-ignore
if (log[kIsBound]) return log;
for (const key in log) {
// @ts-ignore
if (typeof log[key] !== "function") continue;
// @ts-ignore
log[key] = log[key].bind(log);
}
// @ts-ignore
log[kIsBound] = true;
return log;
}
11 changes: 7 additions & 4 deletions src/probot.ts
Expand Up @@ -16,6 +16,7 @@ import type {
State,
} from "./types.js";
import { defaultWebhooksPath } from "./server/server.js";
import { rebindLog } from "./helpers/rebind-log.js";

export type Constructor<T = any> = new (...args: any[]) => T;

Expand Down Expand Up @@ -52,7 +53,9 @@ export class Probot {
let level = options.logLevel;
const logMessageKey = options.logMessageKey;

this.log = options.log || getLog({ level, logMessageKey });
this.log = options.log
? rebindLog(options.log)
: getLog({ level, logMessageKey });

// TODO: support redis backend for access token cache if `options.redisConfig`
const cache = new LRUCache<number, string>({
Expand All @@ -68,11 +71,11 @@ export class Probot {
appId: Number(options.appId),
privateKey: options.privateKey,
cache,
log: this.log,
log: rebindLog(this.log),
redisConfig: options.redisConfig,
baseUrl: options.baseUrl,
});
const octokitLogger = this.log.child({ name: "octokit" });
const octokitLogger = rebindLog(this.log.child({ name: "octokit" }));
const octokit = new Octokit({
request: options.request,
log: {
Expand All @@ -86,7 +89,7 @@ export class Probot {
this.state = {
cache,
githubToken: options.githubToken,
log: this.log,
log: rebindLog(this.log),
Octokit,
octokit,
webhooks: {
Expand Down
5 changes: 4 additions & 1 deletion src/server/server.ts
Expand Up @@ -11,6 +11,7 @@ import { VERSION } from "../version.js";
import type { ApplicationFunction, ServerOptions } from "../types.js";
import type { Probot } from "../index.js";
import type EventSource from "eventsource";
import { rebindLog } from "../helpers/rebind-log.js";

// the default path as defined in @octokit/webhooks
export const defaultWebhooksPath = "/api/github/webhooks";
Expand Down Expand Up @@ -40,7 +41,9 @@ export class Server {
this.probotApp = new options.Probot({
request: options.request,
});
this.log = options.log || this.probotApp.log.child({ name: "server" });
this.log = options.log
? rebindLog(options.log)
: rebindLog(this.probotApp.log.child({ name: "server" }));

Check warning on line 46 in src/server/server.ts

View check run for this annotation

Codecov / codecov/patch

src/server/server.ts#L46

Added line #L46 was not covered by tests

this.state = {
cwd: options.cwd || process.cwd(),
Expand Down

0 comments on commit e25f1ae

Please sign in to comment.