Skip to content

Commit c8801c5

Browse files
committed
Merge branch 'master' into staging/v7
2 parents 99ceb15 + 17d6eb1 commit c8801c5

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
LIT_CURVE,
2222
LIT_CURVE_VALUES,
2323
LIT_ENDPOINT,
24+
LIT_ERROR,
2425
LIT_ERROR_CODE,
2526
LIT_NETWORK,
2627
LIT_NETWORKS,
@@ -74,6 +75,7 @@ import {
7475
} from '@lit-protocol/types';
7576

7677
import { composeLitUrl } from './endpoint-version';
78+
import { LogLevel } from '@lit-protocol/logger';
7779

7880
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7981
type Listener = (...args: any[]) => void;
@@ -199,7 +201,10 @@ export class LitCore {
199201

200202
// -- set global variables
201203
globalThis.litConfig = this.config;
202-
bootstrapLogManager('core');
204+
bootstrapLogManager(
205+
'core',
206+
this.config.debug ? LogLevel.DEBUG : LogLevel.OFF
207+
);
203208

204209
// -- configure local storage if not present
205210
// LitNodeClientNodejs is a base for LitNodeClient
@@ -674,7 +679,17 @@ export class LitCore {
674679
this.config.bootstrapUrls.length
675680
} nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
676681

677-
reject(new InitError({}, msg));
682+
try {
683+
// TODO: Kludge, replace with standard error construction
684+
throwError({
685+
message: msg,
686+
errorKind: LIT_ERROR.INIT_ERROR.kind,
687+
errorCode: LIT_ERROR.INIT_ERROR.name,
688+
});
689+
} catch (e) {
690+
logErrorWithRequestId(requestId, e);
691+
reject(e);
692+
}
678693
}, this.config.connectTimeout);
679694
}),
680695
Promise.all(

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: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { version, LOG_LEVEL, LOG_LEVEL_VALUES } from '@lit-protocol/constants';
22
import { hashMessage } from 'ethers/lib/utils';
33

4-
export { LOG_LEVEL };
4+
5+
export enum LogLevel {
6+
OFF = -1,
7+
ERROR = 0,
8+
INFO = 1,
9+
DEBUG = 2,
10+
WARN = 3,
11+
FATAL = 4,
12+
TIMING_START = 5,
13+
TIMING_END = 6,
14+
}
515

616
const colours = {
717
reset: '\x1b[0m',
@@ -197,6 +207,7 @@ export class Logger {
197207
private _config: Record<string, any> | undefined;
198208
private _isParent: boolean;
199209
private _children: Map<string, Logger>;
210+
private _timestamp: number;
200211

201212
public static createLogger(
202213
category: string,
@@ -222,6 +233,7 @@ export class Logger {
222233
this._config = config;
223234
this._children = new Map();
224235
this._isParent = isParent;
236+
this._timestamp = Date.now();
225237
}
226238

227239
get id(): string {
@@ -232,6 +244,10 @@ export class Logger {
232244
return this._category;
233245
}
234246

247+
get timestamp(): number {
248+
return this._timestamp;
249+
}
250+
235251
get Logs(): Log[] {
236252
return this._logs;
237253
}
@@ -305,14 +321,17 @@ export class Logger {
305321

306322
const arrayLog = log.toArray();
307323
if (this._config?.['condenseLogs'] && !this._checkHash(log)) {
308-
(this._level >= level || level === LOG_LEVEL.ERROR) &&
324+
(this._level >= level || level === LogLevel.ERROR) &&
325+
this._consoleHandler &&
309326
this._consoleHandler(...arrayLog);
310327
(this._level >= level || level === LOG_LEVEL.ERROR) &&
311328
this._handler &&
312329
this._handler(log);
313-
(this._level >= level || level === LOG_LEVEL.ERROR) && this._addLog(log);
330+
331+
(this._level >= level || level === LogLevel.ERROR) && this._addLog(log);
314332
} else if (!this._config?.['condenseLogs']) {
315-
(this._level >= level || level === LOG_LEVEL.ERROR) &&
333+
(this._level >= level || level === LogLevel.ERROR) &&
334+
this._consoleHandler &&
316335
this._consoleHandler(...arrayLog);
317336
(this._level >= level || level === LOG_LEVEL.ERROR) &&
318337
this._handler &&
@@ -336,7 +355,6 @@ export class Logger {
336355

337356
private _addLog(log: Log) {
338357
this._logs.push(log);
339-
340358
// TODO: currently we are not deleting old request id's which over time will fill local storage as the maximum storage size is 10mb
341359
// 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
342360
// 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.
@@ -421,14 +439,20 @@ export class LogManager {
421439
}
422440

423441
get LoggerIds(): string[] {
424-
const keys: string[] = [];
442+
const keys: [string, number][] = [];
425443
for (const category of this._loggers.entries()) {
426444
for (const child of category[1].Children) {
427-
keys.push(child[0]);
445+
keys.push([child[0], child[1].timestamp]);
428446
}
429447
}
430448

431-
return keys;
449+
return keys
450+
.sort((a: [string, number], b: [string, number]) => {
451+
return a[1] - b[1];
452+
})
453+
.map((value: [string, number]) => {
454+
return value[0];
455+
});
432456
}
433457

434458
// 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
@@ -154,11 +154,6 @@ export const log = (...args: any): void => {
154154
return;
155155
}
156156

157-
if (globalThis?.litConfig?.debug !== true) {
158-
return;
159-
}
160-
// config is loaded, and debug is true
161-
162157
// if there are there are logs in buffer, print them first and empty the buffer.
163158
while (logBuffer.length > 0) {
164159
const log = logBuffer.shift() ?? '';
@@ -182,11 +177,6 @@ export const logWithRequestId = (id: string, ...args: any) => {
182177
return;
183178
}
184179

185-
if (globalThis?.litConfig?.debug !== true) {
186-
return;
187-
}
188-
// config is loaded, and debug is true
189-
190180
// if there are there are logs in buffer, print them first and empty the buffer.
191181
while (logBuffer.length > 0) {
192182
const log = logBuffer.shift() ?? '';
@@ -212,11 +202,6 @@ export const logErrorWithRequestId = (id: string, ...args: any) => {
212202
return;
213203
}
214204

215-
if (globalThis?.litConfig?.debug !== true) {
216-
return;
217-
}
218-
// config is loaded, and debug is true
219-
220205
// if there are there are logs in buffer, print them first and empty the buffer.
221206
while (logBuffer.length > 0) {
222207
const log = logBuffer.shift() ?? '';
@@ -242,11 +227,6 @@ export const logError = (...args: any) => {
242227
return;
243228
}
244229

245-
if (globalThis?.litConfig?.debug !== true) {
246-
return;
247-
}
248-
// config is loaded, and debug is true
249-
250230
// if there are there are logs in buffer, print them first and empty the buffer.
251231
while (logBuffer.length > 0) {
252232
const log = logBuffer.shift() ?? '';

0 commit comments

Comments
 (0)