generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced logger to have better control of logging
- Loading branch information
1 parent
7cabd6c
commit 959e342
Showing
6 changed files
with
178 additions
and
15 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
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,73 @@ | ||
import { pino, LoggerOptions } from 'pino'; | ||
|
||
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 pinoLogger; | ||
|
||
public constructor() { | ||
const loggerOptions: LoggerOptions = { | ||
level: 'silent', // Default to 'silent' log level | ||
}; | ||
|
||
this.pinoLogger = pino(loggerOptions); | ||
} | ||
|
||
setLogLevel(logLevel: Web5LogLevel): void { | ||
this.pinoLogger.level = logLevel; | ||
} | ||
|
||
public log(message: string): void { | ||
this.info(message); | ||
} | ||
|
||
public info(message: string): void { | ||
this.pinoLogger.info(message); | ||
} | ||
|
||
public error(message: string): void { | ||
this.pinoLogger.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 | ||
} | ||
Oops, something went wrong.