Skip to content

Commit 293e6d8

Browse files
committed
Adding some comments
1 parent aa2e4c6 commit 293e6d8

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

packages/logger/src/logger.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
*/
2727
export const instances: Logger[] = [];
2828

29+
/**
30+
* The JS SDK supports 5 log levels and also allows a user the ability to
31+
* silence the logs altogether.
32+
*
33+
* The order is a follows:
34+
* DEBUG < VERBOSE < INFO < WARN < ERROR < SILENT
35+
*
36+
* All of the log types above the current log level will be captured (i.e. if
37+
* I set the log level to `INFO`, errors will still be logged, but `DEBUG` and
38+
* `VERBOSE` logs will not)
39+
*/
2940
export enum LogLevel {
3041
DEBUG,
3142
VERBOSE,
@@ -35,11 +46,21 @@ export enum LogLevel {
3546
SILENT
3647
}
3748

38-
export type LogHandler = (type: LogLevel, level: LogLevel, ...args: any[]) => void;
49+
/**
50+
* We allow users the ability to pass their own log handler. We will pass the
51+
* type of log, the current log level, and any other arguments passed (i.e. the
52+
* messages that the user wants to log) to this function.
53+
*/
54+
export type LogHandler = (logType: LogLevel, currentLogLevel: LogLevel, ...args: any[]) => void;
3955

40-
const defaultLogHandler: LogHandler = (type: LogLevel, level: LogLevel, ...args: any[]) => {
41-
if (type < level) return;
42-
switch (type) {
56+
/**
57+
* The default log handler will forward DEBUG, VERBOSE, INFO, WARN, and ERROR
58+
* messages on to their corresponding console counterparts (if the log method
59+
* is supported by the current log level)
60+
*/
61+
const defaultLogHandler: LogHandler = (logType: LogLevel, currentLevel: LogLevel, ...args: any[]) => {
62+
if (logType < currentLevel) return;
63+
switch (logType) {
4364
case LogLevel.SILENT:
4465
return;
4566
case LogLevel.VERBOSE:
@@ -67,6 +88,11 @@ export class Logger {
6788
*/
6889
instances.push(this);
6990
}
91+
92+
/**
93+
* The log level of the given logger. Though all of the log levels can be
94+
* centrally set, each logger can be set individually if it desires.
95+
*/
7096
private _logLevel = LogLevel.WARN;
7197
get logLevel() {
7298
return this._logLevel;
@@ -78,6 +104,11 @@ export class Logger {
78104
this._logLevel = val;
79105
}
80106

107+
/**
108+
* The log handler for the current logger instance. This can be set to any
109+
* function value, though this should not be needed the vast majority of the
110+
* time
111+
*/
81112
private _logHandler: LogHandler = defaultLogHandler;
82113
get logHandler() {
83114
return this._logHandler;
@@ -89,6 +120,10 @@ export class Logger {
89120
this._logHandler = val;
90121
}
91122

123+
/**
124+
* The functions below are all based on the `console` interface
125+
*/
126+
92127
debug(...args) {
93128
this._logHandler(LogLevel.DEBUG, this._logLevel, ...args);
94129
}

0 commit comments

Comments
 (0)