From 606840ff115d00f4b16898a033f36a4098058b7e Mon Sep 17 00:00:00 2001 From: Josh Long Date: Mon, 30 Sep 2024 14:25:31 -0400 Subject: [PATCH 1/6] dev: allow request id aggregation if logging is disabled --- packages/core/src/lib/lit-core.ts | 4 +++- packages/logger/src/lib/logger.spec.ts | 14 +++++++++++- packages/logger/src/lib/logger.ts | 30 +++++++++++++++++--------- packages/misc/src/lib/misc.ts | 14 ++---------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/packages/core/src/lib/lit-core.ts b/packages/core/src/lib/lit-core.ts index 523a50d252..d2e83c584b 100644 --- a/packages/core/src/lib/lit-core.ts +++ b/packages/core/src/lib/lit-core.ts @@ -71,6 +71,7 @@ import { } from '@lit-protocol/types'; import { composeLitUrl } from './endpoint-version'; +import { LogLevel } from '@lit-protocol/logger'; // eslint-disable-next-line @typescript-eslint/no-explicit-any type Listener = (...args: any[]) => void; @@ -202,7 +203,7 @@ export class LitCore { // -- set global variables globalThis.litConfig = this.config; - bootstrapLogManager('core'); + bootstrapLogManager('core', this.config.debug ? LogLevel.DEBUG : LogLevel.OFF); // -- configure local storage if not present // LitNodeClientNodejs is a base for LitNodeClient @@ -694,6 +695,7 @@ export class LitCore { errorCode: LIT_ERROR.INIT_ERROR.name, }); } catch (e) { + logErrorWithRequestId(requestId, e); reject(e); } }, this.config.connectTimeout); diff --git a/packages/logger/src/lib/logger.spec.ts b/packages/logger/src/lib/logger.spec.ts index a0f18c3ccd..cfa72d8d48 100644 --- a/packages/logger/src/lib/logger.spec.ts +++ b/packages/logger/src/lib/logger.spec.ts @@ -111,6 +111,18 @@ describe('logger', () => { } expect(lm.getLogsForId('foo7').length).toEqual(count); - expect(lm.LoggerIds.size).toEqual(1); + expect(lm.LoggerIds.length).toEqual(10); + }); + + it('should order logs based on logger creation timestamp', async () => { + const loggerA = lm.get('a', '1'); + await new Promise((res) => setTimeout(res, 100)); + const loggerB = lm.get('b', '2'); + + const requestIds = lm.LoggerIds; + + expect(requestIds.length).toBe(2); + expect(loggerA.timestamp).toEqual(requestIds[0]); + expect(loggerB.timestamp).toEqual(requestIds[1]); }); }); diff --git a/packages/logger/src/lib/logger.ts b/packages/logger/src/lib/logger.ts index 39de057564..f26008dd03 100644 --- a/packages/logger/src/lib/logger.ts +++ b/packages/logger/src/lib/logger.ts @@ -3,10 +3,10 @@ import { hashMessage } from 'ethers/lib/utils'; import { version } from '@lit-protocol/constants'; export enum LogLevel { - INFO = 0, - DEBUG = 1, - WARN = 2, - ERROR = 3, + ERROR = 0, + INFO = 1, + DEBUG = 2, + WARN = 3, FATAL = 4, TIMING_START = 5, TIMING_END = 6, @@ -207,6 +207,7 @@ export class Logger { private _config: Record | undefined; private _isParent: boolean; private _children: Map; + private _timestamp: number; public static createLogger( category: string, @@ -232,6 +233,7 @@ export class Logger { this._config = config; this._children = new Map(); this._isParent = isParent; + this._timestamp = Date.now(); } get id(): string { @@ -242,6 +244,10 @@ export class Logger { return this._category; } + get timestamp(): number { + return this._timestamp; + } + get Logs(): Log[] { return this._logs; } @@ -312,14 +318,15 @@ export class Logger { const arrayLog = log.toArray(); if (this._config?.['condenseLogs'] && !this._checkHash(log)) { (this._level >= level || level === LogLevel.ERROR) && - this._consoleHandler(...arrayLog); + this._consoleHandler && this._consoleHandler(...arrayLog); (this._level >= level || level === LogLevel.ERROR) && this._handler && this._handler(log); + (this._level >= level || level === LogLevel.ERROR) && this._addLog(log); } else if (!this._config?.['condenseLogs']) { (this._level >= level || level === LogLevel.ERROR) && - this._consoleHandler(...arrayLog); + this._consoleHandler && this._consoleHandler(...arrayLog); (this._level >= level || level === LogLevel.ERROR) && this._handler && this._handler(log); @@ -342,7 +349,6 @@ export class Logger { private _addLog(log: Log) { this._logs.push(log); - // TODO: currently we are not deleting old request id's which over time will fill local storage as the maximum storage size is 10mb // we should be deleting keys from the front of the collection of `Object.keys(category)` such that the first keys entered are deleted when we reach a pre defined key threshold // this implementation assumes that serialization / deserialization from `localStorage` keeps the same key ordering in each `category` object as we will asssume the array produced from `Object.keys` will always be the same ordering. @@ -427,14 +433,18 @@ export class LogManager { } get LoggerIds(): string[] { - const keys: string[] = []; + const keys: [string, number][] = []; for (const category of this._loggers.entries()) { for (const child of category[1].Children) { - keys.push(child[0]); + keys.push([child[0], child[1].timestamp]); } } - return keys; + return keys.sort((a: [string, number], b: [string, number]) => { + return a[1] > b[1] ? a[1] : b[1]; + }).map((value: [string, number]) => { + return value[0]; + }); } // if a logger is given an id it will persist logs under its logger instance diff --git a/packages/misc/src/lib/misc.ts b/packages/misc/src/lib/misc.ts index daf1c6e02a..b42124dddd 100644 --- a/packages/misc/src/lib/misc.ts +++ b/packages/misc/src/lib/misc.ts @@ -278,12 +278,7 @@ export const log = (...args: any): void => { logBuffer.push(args); return; } - - if (globalThis?.litConfig?.debug !== true) { - return; - } - // config is loaded, and debug is true - + // if there are there are logs in buffer, print them first and empty the buffer. while (logBuffer.length > 0) { const log = logBuffer.shift() ?? ''; @@ -307,9 +302,6 @@ export const logWithRequestId = (id: string, ...args: any) => { return; } - if (globalThis?.litConfig?.debug !== true) { - return; - } // config is loaded, and debug is true // if there are there are logs in buffer, print them first and empty the buffer. @@ -337,9 +329,7 @@ export const logErrorWithRequestId = (id: string, ...args: any) => { return; } - if (globalThis?.litConfig?.debug !== true) { - return; - } + // config is loaded, and debug is true // if there are there are logs in buffer, print them first and empty the buffer. From 2fcf3e75c3e7910ef1e890f7b7460cf3303dd65f Mon Sep 17 00:00:00 2001 From: Josh Long Date: Mon, 30 Sep 2024 14:35:33 -0400 Subject: [PATCH 2/6] chore: fmt --- packages/core/src/lib/lit-core.ts | 5 ++++- packages/logger/src/lib/logger.ts | 18 +++++++++++------- packages/misc/src/lib/misc.ts | 3 +-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/core/src/lib/lit-core.ts b/packages/core/src/lib/lit-core.ts index d2e83c584b..074fdc107d 100644 --- a/packages/core/src/lib/lit-core.ts +++ b/packages/core/src/lib/lit-core.ts @@ -203,7 +203,10 @@ export class LitCore { // -- set global variables globalThis.litConfig = this.config; - bootstrapLogManager('core', this.config.debug ? LogLevel.DEBUG : LogLevel.OFF); + bootstrapLogManager( + 'core', + this.config.debug ? LogLevel.DEBUG : LogLevel.OFF + ); // -- configure local storage if not present // LitNodeClientNodejs is a base for LitNodeClient diff --git a/packages/logger/src/lib/logger.ts b/packages/logger/src/lib/logger.ts index f26008dd03..68f5d93e26 100644 --- a/packages/logger/src/lib/logger.ts +++ b/packages/logger/src/lib/logger.ts @@ -318,7 +318,8 @@ export class Logger { const arrayLog = log.toArray(); if (this._config?.['condenseLogs'] && !this._checkHash(log)) { (this._level >= level || level === LogLevel.ERROR) && - this._consoleHandler && this._consoleHandler(...arrayLog); + this._consoleHandler && + this._consoleHandler(...arrayLog); (this._level >= level || level === LogLevel.ERROR) && this._handler && this._handler(log); @@ -326,7 +327,8 @@ export class Logger { (this._level >= level || level === LogLevel.ERROR) && this._addLog(log); } else if (!this._config?.['condenseLogs']) { (this._level >= level || level === LogLevel.ERROR) && - this._consoleHandler && this._consoleHandler(...arrayLog); + this._consoleHandler && + this._consoleHandler(...arrayLog); (this._level >= level || level === LogLevel.ERROR) && this._handler && this._handler(log); @@ -440,11 +442,13 @@ export class LogManager { } } - return keys.sort((a: [string, number], b: [string, number]) => { - return a[1] > b[1] ? a[1] : b[1]; - }).map((value: [string, number]) => { - return value[0]; - }); + return keys + .sort((a: [string, number], b: [string, number]) => { + return a[1] > b[1] ? a[1] : b[1]; + }) + .map((value: [string, number]) => { + return value[0]; + }); } // if a logger is given an id it will persist logs under its logger instance diff --git a/packages/misc/src/lib/misc.ts b/packages/misc/src/lib/misc.ts index b42124dddd..8a82543603 100644 --- a/packages/misc/src/lib/misc.ts +++ b/packages/misc/src/lib/misc.ts @@ -278,7 +278,7 @@ export const log = (...args: any): void => { logBuffer.push(args); return; } - + // if there are there are logs in buffer, print them first and empty the buffer. while (logBuffer.length > 0) { const log = logBuffer.shift() ?? ''; @@ -329,7 +329,6 @@ export const logErrorWithRequestId = (id: string, ...args: any) => { return; } - // config is loaded, and debug is true // if there are there are logs in buffer, print them first and empty the buffer. From d0f36779cfcb156aa24c85bca16498b04f66e00e Mon Sep 17 00:00:00 2001 From: Josh Long Date: Mon, 30 Sep 2024 14:40:35 -0400 Subject: [PATCH 3/6] ref: change enum ordering --- packages/logger/src/lib/logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/lib/logger.ts b/packages/logger/src/lib/logger.ts index 68f5d93e26..2f1c7ecd71 100644 --- a/packages/logger/src/lib/logger.ts +++ b/packages/logger/src/lib/logger.ts @@ -3,6 +3,7 @@ import { hashMessage } from 'ethers/lib/utils'; import { version } from '@lit-protocol/constants'; export enum LogLevel { + OFF = -1, ERROR = 0, INFO = 1, DEBUG = 2, @@ -10,7 +11,6 @@ export enum LogLevel { FATAL = 4, TIMING_START = 5, TIMING_END = 6, - OFF = -1, } const colours = { From 09f2086040dc6a6f426f914e92d3dd6be2e3dffd Mon Sep 17 00:00:00 2001 From: Josh Long Date: Wed, 2 Oct 2024 09:03:24 -0400 Subject: [PATCH 4/6] ref: remove checks on config flag --- packages/misc/src/lib/misc.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/misc/src/lib/misc.ts b/packages/misc/src/lib/misc.ts index 8a82543603..e91d0b5f5b 100644 --- a/packages/misc/src/lib/misc.ts +++ b/packages/misc/src/lib/misc.ts @@ -302,8 +302,6 @@ export const logWithRequestId = (id: string, ...args: any) => { return; } - // config is loaded, and debug is true - // if there are there are logs in buffer, print them first and empty the buffer. while (logBuffer.length > 0) { const log = logBuffer.shift() ?? ''; @@ -329,8 +327,6 @@ export const logErrorWithRequestId = (id: string, ...args: any) => { return; } - // config is loaded, and debug is true - // if there are there are logs in buffer, print them first and empty the buffer. while (logBuffer.length > 0) { const log = logBuffer.shift() ?? ''; @@ -356,11 +352,6 @@ export const logError = (...args: any) => { return; } - if (globalThis?.litConfig?.debug !== true) { - return; - } - // config is loaded, and debug is true - // if there are there are logs in buffer, print them first and empty the buffer. while (logBuffer.length > 0) { const log = logBuffer.shift() ?? ''; From bd65454c67615d444b2b7eb9122ee947812ddab4 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Wed, 2 Oct 2024 09:45:57 -0400 Subject: [PATCH 5/6] ref: fix sort compareFn --- packages/logger/src/lib/logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/lib/logger.ts b/packages/logger/src/lib/logger.ts index 2f1c7ecd71..adc697b09c 100644 --- a/packages/logger/src/lib/logger.ts +++ b/packages/logger/src/lib/logger.ts @@ -444,7 +444,7 @@ export class LogManager { return keys .sort((a: [string, number], b: [string, number]) => { - return a[1] > b[1] ? a[1] : b[1]; + return a[1] - b[1]; }) .map((value: [string, number]) => { return value[0]; From 47860e492c0efff5fe8c899719425d2e67906958 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Wed, 2 Oct 2024 09:50:27 -0400 Subject: [PATCH 6/6] chore: fmt --- packages/logger/src/lib/logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/logger/src/lib/logger.ts b/packages/logger/src/lib/logger.ts index adc697b09c..5c2e0a94f7 100644 --- a/packages/logger/src/lib/logger.ts +++ b/packages/logger/src/lib/logger.ts @@ -444,7 +444,7 @@ export class LogManager { return keys .sort((a: [string, number], b: [string, number]) => { - return a[1] - b[1]; + return a[1] - b[1]; }) .map((value: [string, number]) => { return value[0];