-
Notifications
You must be signed in to change notification settings - Fork 88
Feature/lit 3911 add request id aggregation to logger when logging is disabled #661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
606840f
2fcf3e7
d0f3677
f40c203
09f2086
bd65454
47860e4
5bbd27c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<string, any> | undefined; | ||
| private _isParent: boolean; | ||
| private _children: Map<string, Logger>; | ||
| 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]) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the goal here is to sort the array keys based on the timestamp (child[1].timestamp]) right? If so, I think we need to do the following instead, because the sorting callback should return a positive number, a negative number, and a 0 if they are equal. return keys
.sort((a: [string, number], b: [string, number]) => {
return a[1] - b[1];
}) // Corrected sortinghttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! good catch, I had this refactored to the same but I did not push it. Since any value < 0 will result in: |
||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -279,11 +279,6 @@ export const log = (...args: any): void => { | |
| return; | ||
| } | ||
|
|
||
| if (globalThis?.litConfig?.debug !== true) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 Why does removing this check and the one on L310-L312 below not result in logging just being blanket enabled even when debug is set to false?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you look in |
||
| 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,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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: These orderings are not standard. going to come back and re work the level enumeration weights in a fast follow PR with other changes we discussed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use the levels from another lib like ethers?