Skip to content

Commit 17d6eb1

Browse files
authored
Merge pull request #661 from LIT-Protocol/feature/lit-3911-add-request-id-aggregation-to-logger-when-logging-is-diabled
Feature/lit 3911 add request id aggregation to logger when logging is disabled
2 parents 535736b + 5bbd27c commit 17d6eb1

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

packages/core/src/lib/lit-core.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import {
7171
} from '@lit-protocol/types';
7272

7373
import { composeLitUrl } from './endpoint-version';
74+
import { LogLevel } from '@lit-protocol/logger';
7475

7576
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7677
type Listener = (...args: any[]) => void;
@@ -202,7 +203,10 @@ export class LitCore {
202203

203204
// -- set global variables
204205
globalThis.litConfig = this.config;
205-
bootstrapLogManager('core');
206+
bootstrapLogManager(
207+
'core',
208+
this.config.debug ? LogLevel.DEBUG : LogLevel.OFF
209+
);
206210

207211
// -- configure local storage if not present
208212
// LitNodeClientNodejs is a base for LitNodeClient
@@ -694,6 +698,7 @@ export class LitCore {
694698
errorCode: LIT_ERROR.INIT_ERROR.name,
695699
});
696700
} catch (e) {
701+
logErrorWithRequestId(requestId, e);
697702
reject(e);
698703
}
699704
}, this.config.connectTimeout);

packages/logger/src/lib/logger.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ describe('logger', () => {
111111
}
112112

113113
expect(lm.getLogsForId('foo7').length).toEqual(count);
114-
expect(lm.LoggerIds.size).toEqual(1);
114+
expect(lm.LoggerIds.length).toEqual(10);
115+
});
116+
117+
it('should order logs based on logger creation timestamp', async () => {
118+
const loggerA = lm.get('a', '1');
119+
await new Promise((res) => setTimeout(res, 100));
120+
const loggerB = lm.get('b', '2');
121+
122+
const requestIds = lm.LoggerIds;
123+
124+
expect(requestIds.length).toBe(2);
125+
expect(loggerA.timestamp).toEqual(requestIds[0]);
126+
expect(loggerB.timestamp).toEqual(requestIds[1]);
115127
});
116128
});

packages/logger/src/lib/logger.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { hashMessage } from 'ethers/lib/utils';
33
import { version } from '@lit-protocol/constants';
44

55
export enum LogLevel {
6-
INFO = 0,
7-
DEBUG = 1,
8-
WARN = 2,
9-
ERROR = 3,
6+
OFF = -1,
7+
ERROR = 0,
8+
INFO = 1,
9+
DEBUG = 2,
10+
WARN = 3,
1011
FATAL = 4,
1112
TIMING_START = 5,
1213
TIMING_END = 6,
13-
OFF = -1,
1414
}
1515

1616
const colours = {
@@ -207,6 +207,7 @@ export class Logger {
207207
private _config: Record<string, any> | undefined;
208208
private _isParent: boolean;
209209
private _children: Map<string, Logger>;
210+
private _timestamp: number;
210211

211212
public static createLogger(
212213
category: string,
@@ -232,6 +233,7 @@ export class Logger {
232233
this._config = config;
233234
this._children = new Map();
234235
this._isParent = isParent;
236+
this._timestamp = Date.now();
235237
}
236238

237239
get id(): string {
@@ -242,6 +244,10 @@ export class Logger {
242244
return this._category;
243245
}
244246

247+
get timestamp(): number {
248+
return this._timestamp;
249+
}
250+
245251
get Logs(): Log[] {
246252
return this._logs;
247253
}
@@ -312,13 +318,16 @@ export class Logger {
312318
const arrayLog = log.toArray();
313319
if (this._config?.['condenseLogs'] && !this._checkHash(log)) {
314320
(this._level >= level || level === LogLevel.ERROR) &&
321+
this._consoleHandler &&
315322
this._consoleHandler(...arrayLog);
316323
(this._level >= level || level === LogLevel.ERROR) &&
317324
this._handler &&
318325
this._handler(log);
326+
319327
(this._level >= level || level === LogLevel.ERROR) && this._addLog(log);
320328
} else if (!this._config?.['condenseLogs']) {
321329
(this._level >= level || level === LogLevel.ERROR) &&
330+
this._consoleHandler &&
322331
this._consoleHandler(...arrayLog);
323332
(this._level >= level || level === LogLevel.ERROR) &&
324333
this._handler &&
@@ -342,7 +351,6 @@ export class Logger {
342351

343352
private _addLog(log: Log) {
344353
this._logs.push(log);
345-
346354
// TODO: currently we are not deleting old request id's which over time will fill local storage as the maximum storage size is 10mb
347355
// 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
348356
// 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 {
427435
}
428436

429437
get LoggerIds(): string[] {
430-
const keys: string[] = [];
438+
const keys: [string, number][] = [];
431439
for (const category of this._loggers.entries()) {
432440
for (const child of category[1].Children) {
433-
keys.push(child[0]);
441+
keys.push([child[0], child[1].timestamp]);
434442
}
435443
}
436444

437-
return keys;
445+
return keys
446+
.sort((a: [string, number], b: [string, number]) => {
447+
return a[1] - b[1];
448+
})
449+
.map((value: [string, number]) => {
450+
return value[0];
451+
});
438452
}
439453

440454
// if a logger is given an id it will persist logs under its logger instance

packages/misc/src/lib/misc.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,6 @@ export const log = (...args: any): void => {
279279
return;
280280
}
281281

282-
if (globalThis?.litConfig?.debug !== true) {
283-
return;
284-
}
285-
// config is loaded, and debug is true
286-
287282
// if there are there are logs in buffer, print them first and empty the buffer.
288283
while (logBuffer.length > 0) {
289284
const log = logBuffer.shift() ?? '';
@@ -307,11 +302,6 @@ export const logWithRequestId = (id: string, ...args: any) => {
307302
return;
308303
}
309304

310-
if (globalThis?.litConfig?.debug !== true) {
311-
return;
312-
}
313-
// config is loaded, and debug is true
314-
315305
// if there are there are logs in buffer, print them first and empty the buffer.
316306
while (logBuffer.length > 0) {
317307
const log = logBuffer.shift() ?? '';
@@ -337,11 +327,6 @@ export const logErrorWithRequestId = (id: string, ...args: any) => {
337327
return;
338328
}
339329

340-
if (globalThis?.litConfig?.debug !== true) {
341-
return;
342-
}
343-
// config is loaded, and debug is true
344-
345330
// if there are there are logs in buffer, print them first and empty the buffer.
346331
while (logBuffer.length > 0) {
347332
const log = logBuffer.shift() ?? '';
@@ -367,11 +352,6 @@ export const logError = (...args: any) => {
367352
return;
368353
}
369354

370-
if (globalThis?.litConfig?.debug !== true) {
371-
return;
372-
}
373-
// config is loaded, and debug is true
374-
375355
// if there are there are logs in buffer, print them first and empty the buffer.
376356
while (logBuffer.length > 0) {
377357
const log = logBuffer.shift() ?? '';

0 commit comments

Comments
 (0)