generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
101f463
commit a2cd9d5
Showing
4 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Web5 logger level. | ||
*/ | ||
export enum Web5LogLevel { | ||
Debug = 'debug', | ||
Silent = 'silent', | ||
} | ||
|
||
/** | ||
* Web5 logger interface. | ||
*/ | ||
export interface Web5LoggerInterface { | ||
|
||
/** | ||
* Sets the log verbose level. | ||
*/ | ||
setLogLevel(logLevel: Web5LogLevel): void; | ||
|
||
/** | ||
* Same as `info()`. | ||
* Logs an informational message. | ||
*/ | ||
log (message: string): void; | ||
|
||
/** | ||
* Logs an informational message. | ||
*/ | ||
info(message: string): void; | ||
|
||
/** | ||
* Logs an error message. | ||
*/ | ||
error(message: string): void; | ||
} | ||
|
||
/** | ||
* A Web5 logger implementation. | ||
*/ | ||
class Web5Logger implements Web5LoggerInterface { | ||
private logLevel: Web5LogLevel = Web5LogLevel.Silent; // Default to silent/no-op log level | ||
|
||
setLogLevel(logLevel: Web5LogLevel): void { | ||
this.logLevel = logLevel; | ||
} | ||
|
||
public log(message: string): void { | ||
this.info(message); | ||
} | ||
|
||
public info(message: string): void { | ||
if (this.logLevel === Web5LogLevel.Silent) { return; } | ||
|
||
console.info(message); | ||
} | ||
|
||
public error(message: string): void { | ||
if (this.logLevel === Web5LogLevel.Silent) { return; } | ||
|
||
console.error(message); | ||
} | ||
} | ||
|
||
// Export a singleton logger instance | ||
export const logger = new Web5Logger(); | ||
|
||
// Attach logger to the global window object in browser environment for easy access to the logger instance. | ||
// e.g. can call `web5logger.setLogLevel('debug');` directly in browser console. | ||
if (typeof window !== 'undefined') { | ||
(window as any).web5logger = logger; // Makes `web5Logger` accessible globally in browser | ||
} |