Skip to content

Commit

Permalink
Change logger to singleton (#564)
Browse files Browse the repository at this point in the history
Closes PRO-722.

This PR moves the logger to singleton, we can keep all tags centrally. Will be needed for the sessionId change I'm doing.
  • Loading branch information
callingmedic911 authored Jun 27, 2024
1 parent 2185925 commit 6ce3100
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 58 deletions.
3 changes: 2 additions & 1 deletion packages/puppeteer/src/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { initLogger, logger } from "@replay-cli/shared/logger";
import { installLatestRuntimeRelease } from "@replay-cli/shared/runtime/installLatestRuntimeRelease";
import { logger } from "@replay-cli/shared/runtime/logger";

async function install() {
initLogger("puppeteer");
try {
await installLatestRuntimeRelease();
} finally {
Expand Down
3 changes: 0 additions & 3 deletions packages/shared/src/graphql/debug.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/shared/src/graphql/fetchAuthIdsFromGraphQL.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { base64Decode } from "../strings/decode";
import { GraphQLError } from "./GraphQLError";
import { logger } from "./debug";
import { logger } from "../logger";
import { queryGraphQL } from "./queryGraphQL";

export type AuthIds = { userId: string | null; workspaceId: string | null };
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/graphql/queryGraphQL.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fetch } from "undici";
import { replayApiServer } from "../config";
import { getUserAgent } from "../userAgent";
import { logger } from "./debug";
import { logger } from "../logger";

export async function queryGraphQL(name: string, query: string, variables = {}, apiKey?: string) {
const options = {
Expand Down
16 changes: 15 additions & 1 deletion packages/shared/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Logger {
basicAuth: GRAFANA_BASIC_AUTH,
format: winston.format.json(),
replaceTimestamp: true,
timeout: 5000,
onConnectionError: err => this.localDebugger("Grafana connection error", err),
gracefulShutdown: true,
});
Expand Down Expand Up @@ -94,4 +95,17 @@ class Logger {
}
}

export { Logger };
let logger: Logger;

// This should be called with the name once at the entry point.
// For example, with the Playwright plugin, it is called in the Reporter interface constructor.
function initLogger(name: string) {
if (logger) {
console.warn(`Logger already initialized.`);
return logger;
}
logger = new Logger(name);
return logger;
}

export { initLogger, logger };
2 changes: 1 addition & 1 deletion packages/shared/src/runtime/getLatestRuntimeRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import { fetch } from "undici";
import { replayAppHost } from "../config";
import { runtimeMetadata } from "./config";
import { logger } from "./logger";
import { logger } from "../logger/logger";
import { Release } from "./types";

const { architecture, platform, runtime } = runtimeMetadata;
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/runtime/getRuntimePath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getReplayPath } from "../getReplayPath";
import { runtimeMetadata } from "./config";
import { logger } from "./logger";
import { logger } from "../logger/logger";

export function getRuntimePath() {
const overridePathKey = `REPLAY_CHROMIUM_EXECUTABLE_PATH`;
Expand Down
3 changes: 1 addition & 2 deletions packages/shared/src/runtime/installLatestRuntimeRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { writeToCache } from "../cache";
import { dim, link } from "../theme";
import { metadataPath, runtimeBasePath, runtimeMetadata } from "./config";
import { getLatestRuntimeRelease } from "./getLatestRuntimeRelease";
import { logger } from "./logger";
import { logger } from "../logger/logger";
import { MetadataJSON } from "./types";

const MAX_DOWNLOAD_ATTEMPTS = 5;
Expand Down Expand Up @@ -84,7 +84,6 @@ export async function installLatestRuntimeRelease(): Promise<Result | undefined>
};
} catch (error) {
logger.debug(`Browser installation failed: ${error}`);

// TODO [PRO-711]
// TODO [PRO-712] Add Mixpanel tracking "failed"
}
Expand Down
3 changes: 0 additions & 3 deletions packages/shared/src/runtime/logger.ts

This file was deleted.

Loading

0 comments on commit 6ce3100

Please sign in to comment.