diff --git a/packages/core/src/lib/lit-core.ts b/packages/core/src/lib/lit-core.ts index 523a50d252..074fdc107d 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,10 @@ 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 +698,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..5c2e0a94f7 100644 --- a/packages/logger/src/lib/logger.ts +++ b/packages/logger/src/lib/logger.ts @@ -3,14 +3,14 @@ import { hashMessage } from 'ethers/lib/utils'; import { version } from '@lit-protocol/constants'; export enum LogLevel { - INFO = 0, - DEBUG = 1, - WARN = 2, - ERROR = 3, + OFF = -1, + ERROR = 0, + INFO = 1, + DEBUG = 2, + WARN = 3, FATAL = 4, TIMING_START = 5, TIMING_END = 6, - OFF = -1, } const colours = { @@ -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,13 +318,16 @@ 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._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 && this._consoleHandler(...arrayLog); (this._level >= level || level === LogLevel.ERROR) && this._handler && @@ -342,7 +351,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 +435,20 @@ 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]; + }) + .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..e91d0b5f5b 100644 --- a/packages/misc/src/lib/misc.ts +++ b/packages/misc/src/lib/misc.ts @@ -279,11 +279,6 @@ export const log = (...args: any): void => { 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,11 +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. while (logBuffer.length > 0) { const log = logBuffer.shift() ?? ''; @@ -337,11 +327,6 @@ 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. while (logBuffer.length > 0) { const log = logBuffer.shift() ?? ''; @@ -367,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() ?? '';