Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
fix: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Nov 26, 2023
1 parent 5e22ebb commit c1e4852
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 28 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"probot-app"
],
"scripts": {
"inspect": "node --inspect-brk ./dist/index.js",
"build:ci": "ncc build src/adapters/github/github-actions.ts -o ./",
"build:serverless": "ncc build src/index.ts -o ./",
"build": "tsc",
Expand Down Expand Up @@ -60,6 +61,7 @@
"openai": "^4.2.0",
"prettier": "^2.7.1",
"probot": "^12.2.4",
"smee-client": "^2.0.0",
"tsx": "^3.12.7",
"yaml": "^2.2.2",
"zlib": "^1.0.5"
Expand All @@ -77,6 +79,7 @@
"eslint": "^8.43.0",
"jest": "^29.6.2",
"knip": "^2.33.4",
"octokit": "^3.1.2",
"rimraf": "3.0.2",
"source-map-support": "^0.5.21",
"ts-jest": "^29.1.1",
Expand Down
14 changes: 10 additions & 4 deletions src/adapters/supabase/helpers/pretty-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import util from "util";
/* eslint-disable @typescript-eslint/no-explicit-any */
type PrettyLogsWithOk = "ok" | LogLevel;
export class PrettyLogs {
constructor() {
this.ok = this.ok.bind(this);
this.info = this.info.bind(this);
this.warn = this.warn.bind(this);
this.error = this.error.bind(this);
this.debug = this.debug.bind(this);
this.http = this.http.bind(this);
this.verbose = this.verbose.bind(this);
this.silly = this.silly.bind(this);
}
public error(message: string, metadata?: any) {
this._logWithStack(LogLevel.ERROR, message, metadata);
}
Expand Down Expand Up @@ -59,7 +69,6 @@ export class PrettyLogs {
delete newMetadata.stack;

if (!this._isEmpty(newMetadata)) {
// console.trace(util.inspect(newMetadata, { showHidden: true, depth: null }));
this._log(type, newMetadata);
}

Expand All @@ -68,7 +77,6 @@ export class PrettyLogs {
const colorizedStack = this._colorizeText(prettyStack, Colors.dim);
this._log(type, colorizedStack);
} else if (stack) {
// console.trace({ type: typeof stack, stack });
const prettyStack = this._formatStackTrace((stack as unknown as string[]).join("\n"), 1);
const colorizedStack = this._colorizeText(prettyStack, Colors.dim);
this._log(type, colorizedStack);
Expand Down Expand Up @@ -113,8 +121,6 @@ export class PrettyLogs {

const symbol = defaultSymbols[type];

// console.trace({ type, symbol, message });

// Formatting the message
const messageFormatted =
typeof message === "string"
Expand Down
34 changes: 16 additions & 18 deletions src/adapters/supabase/helpers/tables/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Logs {
private _throttleCount = 0;
private _retryLimit = 0; // Retries disabled by default

console = new PrettyLogs();
static console: PrettyLogs;

private _log({ level, consoleLog, logMessage, metadata, postComment, type }: LogParams): LogReturn | null {
if (this._getNumericLevel(level) > this._maxLevel) return null; // filter out more verbose logs according to maxLevel set in config
Expand Down Expand Up @@ -89,8 +89,6 @@ export class Logs {
// I have mixed feelings on this because it manipulates metadata later possibly without the developer understanding why and where,
// but seems useful for the metadata parser to understand where the comment originated from

// console.trace({ metadata });

if (!metadata) {
metadata = {};
}
Expand Down Expand Up @@ -118,7 +116,7 @@ export class Logs {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LogLevel.VERBOSE,
consoleLog: this.console.ok,
consoleLog: Logs.console.ok,
logMessage: log,
metadata,
postComment,
Expand All @@ -130,7 +128,7 @@ export class Logs {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LogLevel.INFO,
consoleLog: this.console.info,
consoleLog: Logs.console.info,
logMessage: log,
metadata,
postComment,
Expand All @@ -142,7 +140,7 @@ export class Logs {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LogLevel.WARN,
consoleLog: this.console.warn,
consoleLog: Logs.console.warn,
logMessage: log,
metadata,
postComment,
Expand All @@ -154,7 +152,7 @@ export class Logs {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LogLevel.DEBUG,
consoleLog: this.console.debug,
consoleLog: Logs.console.debug,
logMessage: log,
metadata,
postComment,
Expand All @@ -179,7 +177,7 @@ export class Logs {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LogLevel.ERROR,
consoleLog: this.console.error,
consoleLog: Logs.console.error,
logMessage: log,
metadata,
postComment,
Expand All @@ -191,7 +189,7 @@ export class Logs {
// metadata = this._addDiagnosticInformation(metadata);
// return this._log({
// level: LogLevel.HTTP,
// consoleLog: this.console.http,
// consoleLog: Logs.console.http,
// logMessage: log,
// metadata,
// postComment,
Expand All @@ -203,7 +201,7 @@ export class Logs {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LogLevel.VERBOSE,
consoleLog: this.console.verbose,
consoleLog: Logs.console.verbose,
logMessage: log,
metadata,
postComment,
Expand All @@ -215,7 +213,7 @@ export class Logs {
// metadata = this._addDiagnosticInformation(metadata);
// return this._log({
// level: LogLevel.SILLY,
// consoleLog: this.console.silly,
// consoleLog: Logs.console.silly,
// logMessage: log,
// metadata,
// postComment,
Expand All @@ -235,25 +233,26 @@ export class Logs {
this._environment = environment;
this._retryLimit = retryLimit;
this._maxLevel = this._getNumericLevel(logLevel);
Logs.console = new PrettyLogs();
}

private async _sendLogsToSupabase(log: LogInsert) {
const { error } = await this._supabase.from("logs").insert(log);
if (error) throw this.console.error("Error logging to Supabase:", error);
if (error) throw Logs.console.error("Error logging to Supabase:", error);
}

private async _processLogs(log: LogInsert) {
try {
await this._sendLogsToSupabase(log);
} catch (error) {
this.console.error("Error sending log, retrying:", error);
Logs.console.error("Error sending log, retrying:", error);
return this._retryLimit > 0 ? await this._retryLog(log) : null;
}
}

private async _retryLog(log: LogInsert, retryCount = 0) {
if (retryCount >= this._retryLimit) {
this.console.error("Max retry limit reached for log:", log);
Logs.console.error("Max retry limit reached for log:", log);
return;
}

Expand All @@ -262,7 +261,7 @@ export class Logs {
try {
await this._sendLogsToSupabase(log);
} catch (error) {
this.console.error("Error sending log (after retry):", error);
Logs.console.error("Error sending log (after retry):", error);
await this._retryLog(log, retryCount + 1);
}
}
Expand Down Expand Up @@ -303,10 +302,10 @@ export class Logs {
private _save(logInsert: LogInsert) {
this._addToQueue(logInsert)
.then(() => void 0)
.catch(() => this.console.error("Error adding logs to queue"));
.catch(() => Logs.console.error("Error adding logs to queue"));

if (this._environment === "development") {
this.console.ok(logInsert.log, logInsert);
Logs.console.ok(logInsert.log, logInsert);
}
}

Expand Down Expand Up @@ -352,7 +351,6 @@ export class Logs {
.map((line) => `@@ ${line} @@`)
.join("\n"); // debug: "@@@@",
} else {
// console.trace("unknown log type", type);
// default to gray
message = message
.split("\n")
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/supabase/helpers/tables/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Wallet extends Super {

private _validateAndGetWalletAddress(userWithWallet: UserWithWallet): string {
// const payload = Runtime.getState().latestEventContext.payload;
// console.trace({ payload, userWithWallet });

if (userWithWallet[0]?.wallets?.address === undefined) throw new Error("Wallet address is undefined");
if (userWithWallet[0]?.wallets?.address === null) throw new Error("Wallet address is null");
return userWithWallet[0]?.wallets?.address;
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/comment/handlers/issue/all-comment-scoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CommentScoring } from "./comment-scoring-rubric";
import { ContributorClassesKeys, ContributorView } from "./contribution-style-types";
import { sortCommentsByClass } from "./filter-comments-by-contribution-type";
import { sortUsersByClass } from "./identify-user-ids";
import { perUserCommentScoring } from "./perUserCommentScoring";
import { perUserCommentScoring } from "./per-user-comment-scoring";

import { ContextIssue } from "./specification-scoring";
export async function allCommentScoring({
Expand Down
3 changes: 1 addition & 2 deletions src/handlers/comment/handlers/issue/issue-closed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Context } from "../../../../types/context";
import { Comment, Issue, Payload, StateReason } from "../../../../types/payload";
import structuredMetadata from "../../../shared/structured-metadata";
import { getCollaboratorsForRepo } from "./get-collaborator-ids-for-repo";
import { getPullRequestComments } from "./getPullRequestComments";
import { getPullRequestComments } from "./get-pull-request-comments";

export async function issueClosed(context: Context) {
// TODO: delegate permit calculation to GitHub Action
Expand Down Expand Up @@ -71,7 +71,6 @@ async function getEssentials(context: Context) {
const issueNumber = issue.number;
return { issue, runtime, logger, issueComments, owner, repository, issueNumber };
}
// console.trace({ totals: util.inspect({ totals }, { showHidden: true, depth: null }) });

interface PreflightChecksParams {
issue: Issue;
Expand Down
1 change: 0 additions & 1 deletion src/handlers/comment/handlers/issue/relevance-scoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,5 @@ function averageSamples(batchResults: (number | Decimal)[][], precision: number)
})
.map((score) => score.toDecimalPlaces(precision));

// console.trace(`${JSON.stringify(batchResults)} -> ${JSON.stringify(averageScores)}`);
return averageScores;
}
2 changes: 1 addition & 1 deletion src/handlers/comment/handlers/issue/scoreSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context } from "../../../../types/context";
import { Comment, Issue, User } from "../../../../types/payload";
import { assigneeScoring as assigneeTaskScoring } from "./assignee-scoring";
import { commentsScoring } from "./evaluate-comments";
import { getPullRequestComments } from "./getPullRequestComments";
import { getPullRequestComments } from "./get-pull-request-comments";
import { UserScoreDetails } from "./issue-shared-types";
import { specificationScoring as issuerSpecificationScoring } from "./specification-scoring";

Expand Down
Loading

0 comments on commit c1e4852

Please sign in to comment.