From ba993d2471945340b8d074b64be5deb0eec111a0 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:46:26 +0100 Subject: [PATCH 01/11] feat: log to supabase --- package.json | 4 ++- src/logs.ts | 78 +++++++++++++++++++++++++++++++++++--------- yarn.lock | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index ad237cf..55e546c 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,9 @@ "ubiquity", "open-source" ], - "dependencies": {}, + "dependencies": { + "@supabase/supabase-js": "^2.45.2" + }, "devDependencies": { "@commitlint/cli": "^18.4.3", "@commitlint/config-conventional": "^18.4.3", diff --git a/src/logs.ts b/src/logs.ts index b6c2548..c435b14 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -1,17 +1,41 @@ +import { createClient, SupabaseClient } from "@supabase/supabase-js"; import { LOG_LEVEL } from "./constants"; import { PrettyLogs } from "./pretty-logs"; import { LogParams, LogReturn, Metadata, LogLevel } from "./types/log-types"; +type SupabaseConfig = { + supabaseKey: string; + supabaseUrl: string; +} | null; + + export class Logs { private _maxLevel = -1; static console: PrettyLogs; + private _supabase: SupabaseClient | null = null; + private pluginName: string; + private _levelsToLog: LogLevel[] = ["fatal"]; + + constructor(level: LogLevel, pluginName: string, postingConfig: SupabaseConfig, levelsToLog: LogLevel[] = ["fatal"]) { + this._maxLevel = this._getNumericLevel(level); + Logs.console = new PrettyLogs(); + this.pluginName = pluginName; + this._levelsToLog = levelsToLog; - private _log({ level, consoleLog, logMessage, metadata, type }: LogParams): LogReturn | null { + if (postingConfig) { + if (postingConfig.supabaseKey && postingConfig.supabaseUrl) { + this._supabase = createClient(postingConfig.supabaseUrl, postingConfig.supabaseKey); + } + } + } + + private _log({ level, consoleLog, logMessage, metadata, type }: LogParams): LogReturn { // filter out more verbose logs according to maxLevel set in config - if (this._getNumericLevel(level) > this._maxLevel) return null; + if (this._getNumericLevel(level) < this._maxLevel) { + consoleLog(logMessage, metadata); + } - consoleLog(logMessage, metadata); - return new LogReturn( + const log = new LogReturn( { raw: logMessage, diff: this._diffColorCommentMessage(type, logMessage), @@ -20,6 +44,12 @@ export class Logs { }, metadata ); + + if (this._levelsToLog.includes(level)) { + this._logToSupabase(log); + } + + return log; } private _addDiagnosticInformation(metadata?: Metadata) { @@ -45,7 +75,7 @@ export class Logs { return metadata; } - public ok(log: string, metadata?: Metadata): LogReturn | null { + public ok(log: string, metadata?: Metadata): LogReturn { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.INFO, @@ -56,7 +86,7 @@ export class Logs { }); } - public info(log: string, metadata?: Metadata): LogReturn | null { + public info(log: string, metadata?: Metadata): LogReturn { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.INFO, @@ -67,7 +97,7 @@ export class Logs { }); } - public error(log: string, metadata?: Metadata): LogReturn | null { + public error(log: string, metadata?: Metadata): LogReturn { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.ERROR, @@ -78,7 +108,7 @@ export class Logs { }); } - public debug(log: string, metadata?: Metadata): LogReturn | null { + public debug(log: string, metadata?: Metadata): LogReturn { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.DEBUG, @@ -89,7 +119,7 @@ export class Logs { }); } - public fatal(log: string, metadata?: Metadata): LogReturn | null { + public fatal(log: string, metadata?: Metadata): LogReturn { if (!metadata) { metadata = Logs.convertErrorsIntoObjects(new Error(log)) as Metadata; const stack = metadata.stack as string[]; @@ -114,7 +144,7 @@ export class Logs { }); } - public verbose(log: string, metadata?: Metadata): LogReturn | null { + public verbose(log: string, metadata?: Metadata): LogReturn { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.VERBOSE, @@ -125,11 +155,6 @@ export class Logs { }); } - constructor(logLevel: LogLevel) { - this._maxLevel = this._getNumericLevel(logLevel); - Logs.console = new PrettyLogs(); - } - private _diffColorCommentMessage(type: string, message: string) { const diffPrefix = { fatal: "-", // - text in red @@ -182,6 +207,7 @@ export class Logs { return -1; } } + static convertErrorsIntoObjects(obj: unknown): Metadata | unknown { // this is a utility function to render native errors in the console, the database, and on GitHub. if (obj instanceof Error) { @@ -198,4 +224,26 @@ export class Logs { } return obj; } + + private async _logToSupabase(log: LogReturn) { + if (!this._supabase) { + return; + } + try { + const { data, error } = await this._supabase.from("logs").insert([ + { + log: log.logMessage.raw, + level: log.logMessage.level, + metadata: { ...log.metadata, caller: this.pluginName }, + }, + ]); + if (error) { + throw error; + } + return data; + } catch (err) { + console.error("Error logging to Supabase:", err); + throw err; + } + } } diff --git a/yarn.lock b/yarn.lock index dd104c6..9cfabce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1323,6 +1323,63 @@ ignore "^5.1.8" p-map "^4.0.0" +"@supabase/auth-js@2.64.4": + version "2.64.4" + resolved "https://registry.yarnpkg.com/@supabase/auth-js/-/auth-js-2.64.4.tgz#f27fdabf1ebd1b532ceb57e8bbe66969ee09cfba" + integrity sha512-9ITagy4WP4FLl+mke1rchapOH0RQpf++DI+WSG2sO1OFOZ0rW3cwAM0nCrMOxu+Zw4vJ4zObc08uvQrXx590Tg== + dependencies: + "@supabase/node-fetch" "^2.6.14" + +"@supabase/functions-js@2.4.1": + version "2.4.1" + resolved "https://registry.yarnpkg.com/@supabase/functions-js/-/functions-js-2.4.1.tgz#373e75f8d3453bacd71fb64f88d7a341d7b53ad7" + integrity sha512-8sZ2ibwHlf+WkHDUZJUXqqmPvWQ3UHN0W30behOJngVh/qHHekhJLCFbh0AjkE9/FqqXtf9eoVvmYgfCLk5tNA== + dependencies: + "@supabase/node-fetch" "^2.6.14" + +"@supabase/node-fetch@2.6.15", "@supabase/node-fetch@^2.6.14": + version "2.6.15" + resolved "https://registry.yarnpkg.com/@supabase/node-fetch/-/node-fetch-2.6.15.tgz#731271430e276983191930816303c44159e7226c" + integrity sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ== + dependencies: + whatwg-url "^5.0.0" + +"@supabase/postgrest-js@1.15.8": + version "1.15.8" + resolved "https://registry.yarnpkg.com/@supabase/postgrest-js/-/postgrest-js-1.15.8.tgz#827aaa408cdbc89e67d0a758e7a545ac86e34312" + integrity sha512-YunjXpoQjQ0a0/7vGAvGZA2dlMABXFdVI/8TuVKtlePxyT71sl6ERl6ay1fmIeZcqxiuFQuZw/LXUuStUG9bbg== + dependencies: + "@supabase/node-fetch" "^2.6.14" + +"@supabase/realtime-js@2.10.2": + version "2.10.2" + resolved "https://registry.yarnpkg.com/@supabase/realtime-js/-/realtime-js-2.10.2.tgz#c2b42d17d723d2d2a9146cfad61dc3df1ce3127e" + integrity sha512-qyCQaNg90HmJstsvr2aJNxK2zgoKh9ZZA8oqb7UT2LCh3mj9zpa3Iwu167AuyNxsxrUE8eEJ2yH6wLCij4EApA== + dependencies: + "@supabase/node-fetch" "^2.6.14" + "@types/phoenix" "^1.5.4" + "@types/ws" "^8.5.10" + ws "^8.14.2" + +"@supabase/storage-js@2.7.0": + version "2.7.0" + resolved "https://registry.yarnpkg.com/@supabase/storage-js/-/storage-js-2.7.0.tgz#9ff322d2c3b141087aa34115cf14205e4980ce75" + integrity sha512-iZenEdO6Mx9iTR6T7wC7sk6KKsoDPLq8rdu5VRy7+JiT1i8fnqfcOr6mfF2Eaqky9VQzhP8zZKQYjzozB65Rig== + dependencies: + "@supabase/node-fetch" "^2.6.14" + +"@supabase/supabase-js@^2.45.2": + version "2.45.2" + resolved "https://registry.yarnpkg.com/@supabase/supabase-js/-/supabase-js-2.45.2.tgz#75df2b651942a1aba2ff7e6b02393d5ab3b86d9e" + integrity sha512-kJKY3ISFusVKQWCP8Kqo20Ebxy2WLp6Ry/Suco0aQsPXH7bvn7clswsdhcfcH/5Tr0MYz/jcCjF0n/27SetiCw== + dependencies: + "@supabase/auth-js" "2.64.4" + "@supabase/functions-js" "2.4.1" + "@supabase/node-fetch" "2.6.15" + "@supabase/postgrest-js" "1.15.8" + "@supabase/realtime-js" "2.10.2" + "@supabase/storage-js" "2.7.0" + "@tsconfig/node10@^1.0.7": version "1.0.11" resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.11.tgz#6ee46400685f130e278128c7b38b7e031ff5b2f2" @@ -1434,11 +1491,23 @@ resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz" integrity sha512-37i+OaWTh9qeK4LSHPsyRC7NahnGotNuZvjLSgcPzblpHB3rrCJxAOgI5gCdKm7coonsaX1Of0ILiTcnZjbfxA== +"@types/phoenix@^1.5.4": + version "1.6.5" + resolved "https://registry.yarnpkg.com/@types/phoenix/-/phoenix-1.6.5.tgz#5654e14ec7ad25334a157a20015996b6d7d2075e" + integrity sha512-xegpDuR+z0UqG9fwHqNoy3rI7JDlvaPh2TY47Fl80oq6g+hXT+c/LEuE43X48clZ6lOfANl5WrPur9fYO1RJ/w== + "@types/stack-utils@^2.0.0": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8" integrity sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw== +"@types/ws@^8.5.10": + version "8.5.12" + resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.12.tgz#619475fe98f35ccca2a2f6c137702d85ec247b7e" + integrity sha512-3tPRkv1EtkDpzlgyKyI8pGsGZAGPEaXeu0DOj5DI25Ja91bdAYddYHbADRYVrZMRbfW+1l5YwXVDKohDJNQxkQ== + dependencies: + "@types/node" "*" + "@types/yargs-parser@*": version "21.0.3" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.3.tgz#815e30b786d2e8f0dcd85fd5bcf5e1a04d008f15" @@ -5522,6 +5591,11 @@ tr46@^1.0.1: dependencies: punycode "^2.1.0" +tr46@~0.0.3: + version "0.0.3" + resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" + integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== + tree-kill@^1.2.2: version "1.2.2" resolved "https://registry.yarnpkg.com/tree-kill/-/tree-kill-1.2.2.tgz#4ca09a9092c88b73a7cdc5e8a01b507b0790a0cc" @@ -5810,11 +5884,24 @@ wcwidth@^1.0.1: dependencies: defaults "^1.0.3" +webidl-conversions@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" + integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== + webidl-conversions@^4.0.2: version "4.0.2" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad" integrity sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg== +whatwg-url@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" + integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw== + dependencies: + tr46 "~0.0.3" + webidl-conversions "^3.0.0" + whatwg-url@^7.0.0: version "7.1.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-7.1.0.tgz#c2c492f1eca612988efd3d2266be1b9fc6170d06" @@ -5914,6 +6001,11 @@ write-file-atomic@^4.0.2: imurmurhash "^0.1.4" signal-exit "^3.0.7" +ws@^8.14.2: + version "8.18.0" + resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc" + integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw== + xml@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5" From 48165f551d4c8f12bc2ad73d38c34c2f82abbf08 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:52:02 +0100 Subject: [PATCH 02/11] chore: cast in one place --- src/logs.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/logs.ts b/src/logs.ts index c435b14..d7bf033 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -31,7 +31,7 @@ export class Logs { private _log({ level, consoleLog, logMessage, metadata, type }: LogParams): LogReturn { // filter out more verbose logs according to maxLevel set in config - if (this._getNumericLevel(level) < this._maxLevel) { + if (this._getNumericLevel(level) <= this._maxLevel) { consoleLog(logMessage, metadata); } @@ -121,14 +121,14 @@ export class Logs { public fatal(log: string, metadata?: Metadata): LogReturn { if (!metadata) { - metadata = Logs.convertErrorsIntoObjects(new Error(log)) as Metadata; + metadata = Logs.convertErrorsIntoObjects(new Error(log)) const stack = metadata.stack as string[]; stack.splice(1, 1); metadata.stack = stack; } if (metadata instanceof Error) { - metadata = Logs.convertErrorsIntoObjects(metadata) as Metadata; + metadata = Logs.convertErrorsIntoObjects(metadata) const stack = metadata.stack as string[]; stack.splice(1, 1); metadata.stack = stack; @@ -208,7 +208,7 @@ export class Logs { } } - static convertErrorsIntoObjects(obj: unknown): Metadata | unknown { + static convertErrorsIntoObjects(obj: unknown): Metadata { // this is a utility function to render native errors in the console, the database, and on GitHub. if (obj instanceof Error) { return { @@ -222,7 +222,7 @@ export class Logs { obj[key] = this.convertErrorsIntoObjects(obj[key]); }); } - return obj; + return obj as Metadata; } private async _logToSupabase(log: LogReturn) { From 3fddab4de0e28a952879a8623ef0ad5ab6a24b3a Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:56:18 +0100 Subject: [PATCH 03/11] chore: place levelsToLog in postingConfig, optionalize --- src/logs.ts | 10 +++++----- tests/logs.test.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/logs.ts b/src/logs.ts index d7bf033..9f8ccea 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -6,23 +6,23 @@ import { LogParams, LogReturn, Metadata, LogLevel } from "./types/log-types"; type SupabaseConfig = { supabaseKey: string; supabaseUrl: string; -} | null; - + levelsToLog: LogLevel[]; +} export class Logs { private _maxLevel = -1; static console: PrettyLogs; private _supabase: SupabaseClient | null = null; private pluginName: string; - private _levelsToLog: LogLevel[] = ["fatal"]; + private _levelsToLog: LogLevel[] = []; - constructor(level: LogLevel, pluginName: string, postingConfig: SupabaseConfig, levelsToLog: LogLevel[] = ["fatal"]) { + constructor(level: LogLevel, pluginName: string, postingConfig?: SupabaseConfig) { this._maxLevel = this._getNumericLevel(level); Logs.console = new PrettyLogs(); this.pluginName = pluginName; - this._levelsToLog = levelsToLog; if (postingConfig) { + this._levelsToLog = postingConfig.levelsToLog; if (postingConfig.supabaseKey && postingConfig.supabaseUrl) { this._supabase = createClient(postingConfig.supabaseUrl, postingConfig.supabaseKey); } diff --git a/tests/logs.test.ts b/tests/logs.test.ts index 73a745f..df71546 100644 --- a/tests/logs.test.ts +++ b/tests/logs.test.ts @@ -6,7 +6,7 @@ describe("Logs", () => { let logs: Logs; beforeEach(() => { - logs = new Logs(LOG_LEVEL.DEBUG); + logs = new Logs(LOG_LEVEL.DEBUG, "test") }); it("should log an 'ok' message", () => { From fd34f13c1f73dccb9d3b857c1800dd224ec8bc73 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Sat, 7 Sep 2024 04:44:26 +0100 Subject: [PATCH 04/11] chore: init with client not key and url, move to devDeps --- package.json | 7 +++---- src/logs.ts | 11 ++++------- yarn.lock | 18 +++++++++--------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 55e546c..960f7b0 100644 --- a/package.json +++ b/package.json @@ -32,12 +32,11 @@ "ubiquity", "open-source" ], - "dependencies": { - "@supabase/supabase-js": "^2.45.2" - }, + "dependencies": {}, "devDependencies": { "@commitlint/cli": "^18.4.3", "@commitlint/config-conventional": "^18.4.3", + "@supabase/supabase-js": "^2.45.3", "@types/jest": "^29.5.12", "@types/node": "^20.10.4", "@typescript-eslint/eslint-plugin": "^7.14.1", @@ -71,4 +70,4 @@ ] }, "packageManager": "yarn@1.22.22" -} +} \ No newline at end of file diff --git a/src/logs.ts b/src/logs.ts index 9f8ccea..81b2848 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -1,11 +1,10 @@ -import { createClient, SupabaseClient } from "@supabase/supabase-js"; +import { SupabaseClient } from "@supabase/supabase-js"; import { LOG_LEVEL } from "./constants"; import { PrettyLogs } from "./pretty-logs"; import { LogParams, LogReturn, Metadata, LogLevel } from "./types/log-types"; type SupabaseConfig = { - supabaseKey: string; - supabaseUrl: string; + supabaseClient: SupabaseClient; levelsToLog: LogLevel[]; } @@ -23,9 +22,7 @@ export class Logs { if (postingConfig) { this._levelsToLog = postingConfig.levelsToLog; - if (postingConfig.supabaseKey && postingConfig.supabaseUrl) { - this._supabase = createClient(postingConfig.supabaseUrl, postingConfig.supabaseKey); - } + this._supabase = postingConfig.supabaseClient; } } @@ -45,7 +42,7 @@ export class Logs { metadata ); - if (this._levelsToLog.includes(level)) { + if (this._supabase && this._levelsToLog.includes(level)) { this._logToSupabase(log); } diff --git a/yarn.lock b/yarn.lock index 9cfabce..f4cd1a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1323,10 +1323,10 @@ ignore "^5.1.8" p-map "^4.0.0" -"@supabase/auth-js@2.64.4": - version "2.64.4" - resolved "https://registry.yarnpkg.com/@supabase/auth-js/-/auth-js-2.64.4.tgz#f27fdabf1ebd1b532ceb57e8bbe66969ee09cfba" - integrity sha512-9ITagy4WP4FLl+mke1rchapOH0RQpf++DI+WSG2sO1OFOZ0rW3cwAM0nCrMOxu+Zw4vJ4zObc08uvQrXx590Tg== +"@supabase/auth-js@2.65.0": + version "2.65.0" + resolved "https://registry.yarnpkg.com/@supabase/auth-js/-/auth-js-2.65.0.tgz#e345c492f8cbc31cd6289968eae0e349ff0f39e9" + integrity sha512-+wboHfZufAE2Y612OsKeVP4rVOeGZzzMLD/Ac3HrTQkkY4qXNjI6Af9gtmxwccE5nFvTiF114FEbIQ1hRq5uUw== dependencies: "@supabase/node-fetch" "^2.6.14" @@ -1368,12 +1368,12 @@ dependencies: "@supabase/node-fetch" "^2.6.14" -"@supabase/supabase-js@^2.45.2": - version "2.45.2" - resolved "https://registry.yarnpkg.com/@supabase/supabase-js/-/supabase-js-2.45.2.tgz#75df2b651942a1aba2ff7e6b02393d5ab3b86d9e" - integrity sha512-kJKY3ISFusVKQWCP8Kqo20Ebxy2WLp6Ry/Suco0aQsPXH7bvn7clswsdhcfcH/5Tr0MYz/jcCjF0n/27SetiCw== +"@supabase/supabase-js@^2.45.3": + version "2.45.3" + resolved "https://registry.yarnpkg.com/@supabase/supabase-js/-/supabase-js-2.45.3.tgz#d32b7a7b379958a10dcce32af1182f82b5b82218" + integrity sha512-4wAux6cuVMrdH/qUjKn6p3p3L9AtAO3Une6ojIrtpCj1RaXKVoyIATiacSRAI+pKff6XZBVCGC29v+z4Jo/uSw== dependencies: - "@supabase/auth-js" "2.64.4" + "@supabase/auth-js" "2.65.0" "@supabase/functions-js" "2.4.1" "@supabase/node-fetch" "2.6.15" "@supabase/postgrest-js" "1.15.8" From a8992b5ece5d5d5ed3c061c724d50edd2dbd1cea Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:14:02 +0100 Subject: [PATCH 05/11] feat: LogReturn extends Error --- package.json | 2 +- src/logs.ts | 14 +++++++------- src/types/log-types.ts | 3 ++- tests/logs.test.ts | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 960f7b0..0fd8f92 100644 --- a/package.json +++ b/package.json @@ -70,4 +70,4 @@ ] }, "packageManager": "yarn@1.22.22" -} \ No newline at end of file +} diff --git a/src/logs.ts b/src/logs.ts index 81b2848..6124418 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -6,19 +6,19 @@ import { LogParams, LogReturn, Metadata, LogLevel } from "./types/log-types"; type SupabaseConfig = { supabaseClient: SupabaseClient; levelsToLog: LogLevel[]; -} +}; export class Logs { private _maxLevel = -1; static console: PrettyLogs; private _supabase: SupabaseClient | null = null; - private pluginName: string; + private _pluginName: string; private _levelsToLog: LogLevel[] = []; constructor(level: LogLevel, pluginName: string, postingConfig?: SupabaseConfig) { this._maxLevel = this._getNumericLevel(level); Logs.console = new PrettyLogs(); - this.pluginName = pluginName; + this._pluginName = pluginName; if (postingConfig) { this._levelsToLog = postingConfig.levelsToLog; @@ -43,7 +43,7 @@ export class Logs { ); if (this._supabase && this._levelsToLog.includes(level)) { - this._logToSupabase(log); + this._logToSupabase(log).catch(this.error); } return log; @@ -118,14 +118,14 @@ export class Logs { public fatal(log: string, metadata?: Metadata): LogReturn { if (!metadata) { - metadata = Logs.convertErrorsIntoObjects(new Error(log)) + metadata = Logs.convertErrorsIntoObjects(new Error(log)); const stack = metadata.stack as string[]; stack.splice(1, 1); metadata.stack = stack; } if (metadata instanceof Error) { - metadata = Logs.convertErrorsIntoObjects(metadata) + metadata = Logs.convertErrorsIntoObjects(metadata); const stack = metadata.stack as string[]; stack.splice(1, 1); metadata.stack = stack; @@ -231,7 +231,7 @@ export class Logs { { log: log.logMessage.raw, level: log.logMessage.level, - metadata: { ...log.metadata, caller: this.pluginName }, + metadata: { ...log.metadata, caller: this._pluginName }, }, ]); if (error) { diff --git a/src/types/log-types.ts b/src/types/log-types.ts index 6f25618..78210e3 100644 --- a/src/types/log-types.ts +++ b/src/types/log-types.ts @@ -25,11 +25,12 @@ interface MetadataInterface { export type Metadata = Partial; -export class LogReturn { +export class LogReturn extends Error { logMessage: LogMessage; metadata?: Metadata; constructor(logMessage: LogMessage, metadata?: Metadata) { + super(logMessage.raw); this.logMessage = logMessage; this.metadata = metadata; } diff --git a/tests/logs.test.ts b/tests/logs.test.ts index df71546..383250d 100644 --- a/tests/logs.test.ts +++ b/tests/logs.test.ts @@ -6,7 +6,7 @@ describe("Logs", () => { let logs: Logs; beforeEach(() => { - logs = new Logs(LOG_LEVEL.DEBUG, "test") + logs = new Logs(LOG_LEVEL.DEBUG, "test"); }); it("should log an 'ok' message", () => { From d038d53d884af482173d5a402bd7d36a8beed3c2 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 22 Oct 2024 03:29:58 +0100 Subject: [PATCH 06/11] chore: Update src/logs.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: アレクサンダー.eth <4975670+0x4007@users.noreply.github.com> --- src/logs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logs.ts b/src/logs.ts index 6124418..ca1fe5a 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -43,7 +43,7 @@ export class Logs { ); if (this._supabase && this._levelsToLog.includes(level)) { - this._logToSupabase(log).catch(this.error); + this._logToSupabase(log).catch(this.error => console.error(this.error)); } return log; From a81fe0940daaea823ef5144558ba256f5d13a9b8 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:18:06 +0100 Subject: [PATCH 07/11] Revert "chore: Update src/logs.ts" This reverts commit d038d53d884af482173d5a402bd7d36a8beed3c2. --- src/logs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logs.ts b/src/logs.ts index ca1fe5a..6124418 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -43,7 +43,7 @@ export class Logs { ); if (this._supabase && this._levelsToLog.includes(level)) { - this._logToSupabase(log).catch(this.error => console.error(this.error)); + this._logToSupabase(log).catch(this.error); } return log; From 5ee5426a1ee642b9c3eae400c00ed88f9071f47d Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:38:12 +0100 Subject: [PATCH 08/11] chore: add bindings, sanity checks, correct catch block --- src/logs.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/logs.ts b/src/logs.ts index 6124418..578f8f1 100644 --- a/src/logs.ts +++ b/src/logs.ts @@ -16,6 +16,18 @@ export class Logs { private _levelsToLog: LogLevel[] = []; constructor(level: LogLevel, pluginName: string, postingConfig?: SupabaseConfig) { + this._log = this._log.bind(this); + this._logToSupabase = this._logToSupabase.bind(this); + this._diffColorCommentMessage = this._diffColorCommentMessage.bind(this); + this._getNumericLevel = this._getNumericLevel.bind(this); + this.ok = this.ok.bind(this); + this.info = this.info.bind(this); + this.error = this.error.bind(this); + this.debug = this.debug.bind(this); + this.fatal = this.fatal.bind(this); + this.verbose = this.verbose.bind(this); + this._addDiagnosticInformation = this._addDiagnosticInformation.bind(this); + this._maxLevel = this._getNumericLevel(level); Logs.console = new PrettyLogs(); this._pluginName = pluginName; @@ -43,7 +55,7 @@ export class Logs { ); if (this._supabase && this._levelsToLog.includes(level)) { - this._logToSupabase(log).catch(this.error); + this._logToSupabase(log).catch((error) => this.error(error.message, { error })); } return log; @@ -153,6 +165,12 @@ export class Logs { } private _diffColorCommentMessage(type: string, message: string) { + if (!message) { + return ""; + } + if (typeof message !== "string") { + message = JSON.stringify(message, null, 2); + } const diffPrefix = { fatal: "-", // - text in red ok: "+", // + text in green From e60827df4d0fdc5af47c333dd713d93e188f921e Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:40:37 +0100 Subject: [PATCH 09/11] chore: remove pkg manager field --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0fd8f92..51157d5 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,5 @@ "extends": [ "@commitlint/config-conventional" ] - }, - "packageManager": "yarn@1.22.22" -} + } +} \ No newline at end of file From e32fe63bdc2012a23c326ea57f815033373b76fa Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:41:03 +0100 Subject: [PATCH 10/11] chore: add test case --- tests/logs.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/logs.test.ts b/tests/logs.test.ts index 383250d..35d4282 100644 --- a/tests/logs.test.ts +++ b/tests/logs.test.ts @@ -1,7 +1,20 @@ +import { SupabaseClient } from "@supabase/supabase-js"; import { LOG_LEVEL } from "../src/constants"; import { Logs } from "../src/logs"; import { LogReturn } from "../src/types/log-types"; +jest.mock("@supabase/supabase-js", () => { + return { + SupabaseClient: jest.fn().mockImplementation(() => { + return { + from: jest.fn().mockImplementation(() => { + throw new Error("This is the error you are looking for."); + }), + }; + }), + }; +}); + describe("Logs", () => { let logs: Logs; @@ -47,4 +60,15 @@ describe("Logs", () => { expect(msg).toHaveProperty("diff"); expect(metadata).toHaveProperty("caller"); }); + + it("should log an error when _logToSupabase throws an error", async () => { + const supabaseClient = new SupabaseClient("test", "test"); + const logger = new Logs(LOG_LEVEL.DEBUG, "test", { levelsToLog: ["fatal"], supabaseClient }); + const spy = jest.spyOn(console, "error"); + logger.fatal("This is not the error you are looking for") + await new Promise((resolve) => setTimeout(resolve, 100)); + + expect(spy).toHaveBeenCalled(); + expect(spy).toHaveBeenLastCalledWith("Error logging to Supabase:", new Error("This is the error you are looking for.")); + }); }); From 0340b70816e3178979fc21dbfaa0bd2ac6e03734 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:41:26 +0100 Subject: [PATCH 11/11] chore: format --- package.json | 2 +- tests/logs.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 51157d5..771f1b6 100644 --- a/package.json +++ b/package.json @@ -69,4 +69,4 @@ "@commitlint/config-conventional" ] } -} \ No newline at end of file +} diff --git a/tests/logs.test.ts b/tests/logs.test.ts index 35d4282..eed6740 100644 --- a/tests/logs.test.ts +++ b/tests/logs.test.ts @@ -65,7 +65,7 @@ describe("Logs", () => { const supabaseClient = new SupabaseClient("test", "test"); const logger = new Logs(LOG_LEVEL.DEBUG, "test", { levelsToLog: ["fatal"], supabaseClient }); const spy = jest.spyOn(console, "error"); - logger.fatal("This is not the error you are looking for") + logger.fatal("This is not the error you are looking for"); await new Promise((resolve) => setTimeout(resolve, 100)); expect(spy).toHaveBeenCalled();