Skip to content

Commit

Permalink
Cleanup/logger (#332)
Browse files Browse the repository at this point in the history
* aggregate logs under categories. allow for cross category tracing

* autogen

* fmt

* change log persistence to use request id's as keys for lookup

* fmt

* fmt

* fix logger creation

* fmt

* fmt

* Update/test token (#336)

* update token symbol

* fmt

* fmt

* fmt
  • Loading branch information
Bean authored Feb 6, 2024
1 parent 7fbb90e commit f167795
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 112 deletions.
62 changes: 30 additions & 32 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions apps/html/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- (DO NOT EDIT!) (HTML) THIS FILE IS AUTOMATICALLY GENERATED FROM tools/scripts/gen-html.mjs Thu, 01 Feb 2024 20:59:26 GMT -->
<!-- (DO NOT EDIT!) (HTML) THIS FILE IS AUTOMATICALLY GENERATED FROM tools/scripts/gen-html.mjs Wed, 31 Jan 2024 14:30:15 GMT -->
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -62,7 +62,7 @@
</style>
</head>
<body>
(HTML) THIS FILE IS AUTOMATICALLY GENERATED FROM tools/scripts/gen-html.mjs Thu, 01 Feb 2024 20:59:26 GMT
(HTML) THIS FILE IS AUTOMATICALLY GENERATED FROM tools/scripts/gen-html.mjs Wed, 31 Jan 2024 14:30:15 GMT

<!-- ==================== ALL EXPORTED VANILLA LIBRARIES ==================== -->
<script src="dist/packages/access-control-conditions-vanilla/access-control-conditions.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion apps/react/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@ pre {
`,
}}
/>
(REACT) THIS FILE IS AUTOMATICALLY GENERATED FROM tools/scripts/gen-react.mjs Thu, 01 Feb 2024 20:59:26 GMT
(REACT) THIS FILE IS AUTOMATICALLY GENERATED FROM tools/scripts/gen-react.mjs Wed, 31 Jan 2024 14:30:16 GMT
<div id="root"></div>
<pre><code id="result"></code></pre>
</>
Expand Down
38 changes: 11 additions & 27 deletions lit.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
"address": "0xeF71c2604f17Ec6Fc13409DF24EfdC440D240d37"
},
"CACHE": {
"network": "cayenne",
"debug": true,
"network": "manzano",
"debug": false,
"sevAttestation": false,
"CONTROLLER_AUTHSIG": {
"sig": "0x1620fb611ab3dc6ac1388feea6edc4cec973a23b280ed0f41440ca602807914e2c57238bf72695b9f6971a470a871d10fe17d9fc6c11ce5ed5f00489dd456dfe1c",
"sig": "0x96ddc8d8c043fa2dd5e3add2c0147a841efdcba41b385a9c2f06b3e30af2581d14c8c14beafa4fa9f928f0e5d41bf6c244297c6beb123096c4bb3f27dbdaa6f01b",
"derivedVia": "web3.eth.personal.sign",
"signedMessage": "localhost wants you to sign in with your Ethereum account:\n0xeF71c2604f17Ec6Fc13409DF24EfdC440D240d37\n\nThis is a test statement. You can put anything you want here.\n\nURI: https://localhost/login\nVersion: 1\nChain ID: 1\nNonce: 0xb75ade4f99b009febf749ec834cf50a98a1adb620cdc8b36815af17360f2038e\nIssued At: 2024-01-26T19:47:49.093Z\nExpiration Time: 2024-01-26T20:47:49.090Z",
"signedMessage": "localhost wants you to sign in with your Ethereum account:\n0xeF71c2604f17Ec6Fc13409DF24EfdC440D240d37\n\nThis is a test statement. You can put anything you want here.\n\nURI: https://localhost/login\nVersion: 1\nChain ID: 1\nNonce: 0x6e94513fa67e0a7afb47d1a9542b2ddb17f2c733ac80e16a30e9c862e46b088c\nIssued At: 2024-01-31T21:38:08.528Z\nExpiration Time: 2024-01-31T22:38:08.521Z",
"address": "0xeF71c2604f17Ec6Fc13409DF24EfdC440D240d37"
},
"CONTROLLER_WALLET": {
Expand All @@ -58,9 +58,7 @@
"_isProvider": true,
"_events": [],
"_emitted": {
"block": -2,
"t:0x462949f6b15916679ee0064c8c8727be8fccfe8f0152961b0e84058ce09c67fe": 1603412,
"t:0x614657b89a19ed2f67a214da6c9643e5ec964c1d2ea8a874a50b64778b586133": 1603413
"block": -2
},
"disableCcipRead": false,
"formatter": {
Expand All @@ -77,41 +75,27 @@
},
"anyNetwork": false,
"_networkPromise": {},
"_maxInternalBlockNumber": 1603411,
"_maxInternalBlockNumber": -1024,
"_lastBlockNumber": -2,
"_maxFilterBlockRange": 10,
"_pollingInterval": 4000,
"_fastQueryDate": 1706298471023,
"_fastQueryDate": 0,
"connection": {
"url": "https://chain-rpc.litprotocol.com/http"
},
"_nextId": 73,
"_nextId": 43,
"_eventLoopCache": {
"detectNetwork": null,
"eth_chainId": null,
"eth_blockNumber": null
"eth_chainId": null
},
"_network": {
"chainId": 175177,
"name": "unknown"
},
"_internalBlockNumber": {},
"_fastBlockNumber": 1603411,
"_fastBlockNumberPromise": {}
}
}
},
"PKP_INFO": {
"tokenId": "0x3e3c60f0463a34bd09a44718981bd730690472e2599e050efa92d4969598bed8",
"publicKey": "04de7a670e32bd9cf043f29ea259e5ba50d5ed85fc146478bca9552bfd5f7d6fee9bb377438c25c8fffd5874eb0ede9ea7e594da0f49221d7024b5fdf0577c212a",
"ethAddress": "0xE7cF6e45927E36D70dc727cAD96240217aBC7A9D"
},
"AUTH_METHOD_PKP_INFO": {
"publicKey": "04d92c9302266d6c9ac5b66feb1355fb910910d43664ef15e52925fd9dcd8228f224956633ab30be19b5293a9d3c1bc2e7a8d110d33757de32ed505911ecafdc38",
"ethAddress": "0xAdd686B8A662Ed22dDAF3966b95fF7eeC34B363f",
"authMethod": {
"authMethodId": "0x170d13600caea2933912f39a0334eca3d22e472be203f937c4bad0213d92ed71",
"authMethodType": 1
}
"publicKey": "04b5caf00c9f5adc9b22ca460b88482bad44ed4fac8ee63014b727cf60efc568dbdfe498a94fbd9cd294d651529f9fe76e057e9736150eea038415b06f64a87939"
}
}
}
2 changes: 1 addition & 1 deletion packages/constants/src/lib/constants/autogen_internal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Last Modified: 2024-02-01 20:57:50
// Last Modified: 2024-01-31 14:28:38
// This file is auto-generated by tools/scripts/gen-internal-dev.mjs
export const INTERNAL_DEV = [
'https://167.114.17.204:443',
Expand Down
2 changes: 1 addition & 1 deletion packages/constants/src/lib/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export const LIT_CHAINS: LITChain<LITEVMChain> = {
contractAddress: null,
chainId: 175177,
name: 'Chronicle - Lit Protocol Testnet',
symbol: 'testLIT',
symbol: 'testLPX',
decimals: 18,
rpcUrls: ['https://chain-rpc.litprotocol.com/http'],
blockExplorerUrls: ['https://chain.litprotocol.com/'],
Expand Down
32 changes: 32 additions & 0 deletions packages/logger/src/lib/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,36 @@ describe('logger', () => {
console.log(lm.getLogsForId('foo3'));
expect(lm.getLogsForId('foo3').length).toEqual(1);
});

it('should trace logs through multiple categories', () => {
const logger = lm.get('info-logger', 'foo4');
logger.setLevel(LogLevel.DEBUG);
const logger2 = lm.get('debug-logger', 'foo4');
logger2.setLevel(LogLevel.DEBUG);
logger2.debug('foo');
logger.debug('bar');
expect(lm.getLogsForId('foo4').length).toEqual(2);
});

it('should not persist logs if level set to OFF', () => {
const count = 1_000;
for (let i = 0; i < count; i++) {
const logger = lm.get('' + i, 'foo4');
logger.setLevel(LogLevel.OFF);
logger.debug(i + '');
}

expect(lm.getLogsForId('foo4').length).toEqual(0);
});

it('should persist logs across categories', async () => {
const count = 1_000;
for (let i = 0; i < count; i++) {
const logger = lm.get('' + i, 'foo4');
logger.setLevel(LogLevel.DEBUG);
logger.debug(i + '');
}

expect(lm.getLogsForId('foo4').length).toEqual(count);
});
});
151 changes: 104 additions & 47 deletions packages/logger/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export enum LogLevel {
FATAL = 4,
TIMING_START = 5,
TIMING_END = 6,
OFF = 5,
OFF = 7,
}

const colours = {
Expand Down Expand Up @@ -206,27 +206,33 @@ export class Logger {
private _logs: Log[] = [];
private _logHashes: Map<string, boolean> = new Map();
private _config: Record<string, any> | undefined;
private _isParent: boolean;
private _children: Map<string, Logger>;

public static createLogger(
category: string,
level: LogLevel,
id: string,
isParent: boolean,
config?: Record<string, any>
): Logger {
return new Logger(category, level, id, config);
return new Logger(category, level, id, isParent, config);
}

private constructor(
category: string,
level: LogLevel,
id: string,
isParent: boolean,
config?: Record<string, any>
) {
this._category = category;
this._level = level;
this._id = id;
this._consoleHandler = _resolveLoggingHandler(this._level);
this._config = config;
this._children = new Map();
this._isParent = isParent;
}

get id(): string {
Expand All @@ -249,6 +255,10 @@ export class Logger {
return this._config;
}

get Children(): Map<string, Logger> {
return this._children;
}

public setLevel(level: LogLevel): void {
this._level = level;
}
Expand Down Expand Up @@ -302,19 +312,19 @@ export class Logger {

const arrayLog = log.toArray();
if (this._config?.['condenseLogs'] && !this._checkHash(log)) {
(this._level >= level || level === LogLevel.ERROR) &&
(this._level <= level || level === LogLevel.ERROR) &&
this._consoleHandler(...arrayLog);
(this._level >= level || level === LogLevel.ERROR) &&
(this._level <= level || level === LogLevel.ERROR) &&
this._handler &&
this._handler(log);
(this._level >= level || level === LogLevel.ERROR) && this._addLog(log);
(this._level <= level || level === LogLevel.ERROR) && this._addLog(log);
} else if (!this._config?.['condenseLogs']) {
(this._level >= level || level === LogLevel.ERROR) &&
(this._level <= level || level === LogLevel.ERROR) &&
this._consoleHandler(...arrayLog);
(this._level >= level || level === LogLevel.ERROR) &&
(this._level <= level || level === LogLevel.ERROR) &&
this._handler &&
this._handler(log);
(this._level >= level || level === LogLevel.ERROR) && this._addLog(log);
(this._level <= level || level === LogLevel.ERROR) && this._addLog(log);
}
}

Expand All @@ -332,23 +342,24 @@ export class Logger {

private _addLog(log: Log) {
this._logs.push(log);
this._addToLocalStorage(log);
log.id && this._addToLocalStorage(log);
}

private _addToLocalStorage(log: Log) {
if (globalThis.localStorage) {
let bucket: any = globalThis.localStorage.getItem(log.id);
let bucket: { [index: string]: string[] } | string | null =
globalThis.localStorage.getItem(log.category);
if (bucket) {
bucket = JSON.parse(bucket);
bucket?.logs.push(log.toString());
globalThis.localStorage.setItem(log.id, _safeStringify(bucket));
bucket = JSON.parse(bucket) as { [index: string]: string[] };
if (!bucket[log.id]) {
bucket[log.id] = [];
}
bucket[log.id].push(log.toString());
globalThis.localStorage.setItem(log.category, _safeStringify(bucket));
} else {
globalThis.localStorage.setItem(
log.id,
_safeStringify({
logs: [log.toString()],
})
);
const bucket: { [index: string]: string[] } = {};
bucket[log.id] = [log.toString()];
globalThis.localStorage.setItem(log.category, _safeStringify(bucket));
}
}
}
Expand Down Expand Up @@ -397,49 +408,95 @@ export class LogManager {

// if a logger is given an id it will persist logs under its logger instance
public get(category: string, id?: string): Logger {
let instance = this._loggers.get(`${category}${id}`);
if (instance !== undefined) {
let instance = this._loggers.get(category);
if (!instance && !id) {
this._loggers.set(
category,
Logger.createLogger(category, this._level ?? LogLevel.INFO, '', true)
);

instance = this._loggers.get(category) as Logger;
instance.Config = this._config;
return instance;
}

const logger = Logger.createLogger(
category,
this._level ?? LogLevel.INFO,
id ?? ''
);
if (id) {
if (!instance) {
this._loggers.set(
category,
Logger.createLogger(category, this._level ?? LogLevel.INFO, '', true)
);

logger.Config = this._config as Record<string, any>;
instance = this._loggers.get(category) as Logger;
instance.Config = this._config;
}
let children = instance?.Children;
let child = children?.get(id);
if (child) {
return child;
}
children?.set(
id,
Logger.createLogger(
category,
this._level ?? LogLevel.INFO,
id ?? '',
true
)
);

child = children?.get(id) as Logger;
child.Config = this._config;
return children?.get(id) as Logger;
// fall through condition for if there is no id for the logger and the category is not yet created.
// ex: LogManager.Instance.get('foo');
} else if (!instance) {
this._loggers.set(
category,
Logger.createLogger(category, this._level ?? LogLevel.INFO, '', true)
);

instance = this._loggers.get(category) as Logger;
instance.Config = this._config;
}

this._loggers.set(`${category}${id}`, logger);
return logger;
return instance as Logger;
}

getById(id: string): Logger | undefined {
for (const logger of this._loggers) {
if (logger[1].id == id) {
return logger[1];
getById(id: string): string[] {
let logStrs: string[] = [];
for (const category of this._loggers.entries()) {
let logger = category[1].Children.get(id);
if (logger) {
let logStr = [];
for (const log of logger.Logs) {
logStr.push(log.toString());
}
logStrs = logStrs.concat(logStr);
}
}

return undefined;
return logStrs;
}

public getLogsForId(id: string): string[] {
if (globalThis.localStorage) {
let bucket: string | null = globalThis.localStorage.getItem(id);
if (bucket !== null) {
let data = JSON.parse(bucket as string);
return data.logs as string[];
}
} else {
let logs = this.getById(id)?.Logs;
let logsStrs = [];
for (const log of logs as Log[]) {
logsStrs.push(log.toString());
let logsForRequest: string[] = this.getById(id);
if (logsForRequest.length < 1 && globalThis.localStorage) {
for (const category of this._loggers.keys()) {
let bucketStr: string | null =
globalThis.localStorage.getItem(category);
let bucket: { [key: string]: string[] } = JSON.parse(
bucketStr as string
);
if (bucket && bucket[id]) {
const logsForId: string[] = bucket[id].filter((log: string) =>
log.includes(id)
);
logsForRequest = logsForId.concat(logsForRequest);
}
}
return logsStrs;
}

return [];
return logsForRequest;
}
}
Loading

0 comments on commit f167795

Please sign in to comment.