26
26
*/
27
27
export const instances : Logger [ ] = [ ] ;
28
28
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
+ */
29
40
export enum LogLevel {
30
41
DEBUG ,
31
42
VERBOSE ,
@@ -35,11 +46,21 @@ export enum LogLevel {
35
46
SILENT
36
47
}
37
48
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 ;
39
55
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 ) {
43
64
case LogLevel . SILENT :
44
65
return ;
45
66
case LogLevel . VERBOSE :
@@ -67,6 +88,11 @@ export class Logger {
67
88
*/
68
89
instances . push ( this ) ;
69
90
}
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
+ */
70
96
private _logLevel = LogLevel . WARN ;
71
97
get logLevel ( ) {
72
98
return this . _logLevel ;
@@ -78,6 +104,11 @@ export class Logger {
78
104
this . _logLevel = val ;
79
105
}
80
106
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
+ */
81
112
private _logHandler : LogHandler = defaultLogHandler ;
82
113
get logHandler ( ) {
83
114
return this . _logHandler ;
@@ -89,6 +120,10 @@ export class Logger {
89
120
this . _logHandler = val ;
90
121
}
91
122
123
+ /**
124
+ * The functions below are all based on the `console` interface
125
+ */
126
+
92
127
debug ( ...args ) {
93
128
this . _logHandler ( LogLevel . DEBUG , this . _logLevel , ...args ) ;
94
129
}
0 commit comments