14
14
* limitations under the License.
15
15
*/
16
16
17
- /**
18
- * This is the file that people using Node.js will actually import. You should
19
- * only include this file if you have something specific about your
20
- * implementation that mandates having a separate entrypoint. Otherwise you can
21
- * just use index.ts
22
- */
23
-
24
17
/**
25
18
* A container for all of the Logger instances
26
19
*/
@@ -31,15 +24,15 @@ export const instances: Logger[] = [];
31
24
* silence the logs altogether.
32
25
*
33
26
* The order is a follows:
34
- * DEBUG < VERBOSE < INFO < WARN < ERROR < SILENT
27
+ * DEBUG < LOG < INFO < WARN < ERROR < SILENT
35
28
*
36
29
* All of the log types above the current log level will be captured (i.e. if
37
30
* I set the log level to `INFO`, errors will still be logged, but `DEBUG` and
38
- * `VERBOSE ` logs will not)
31
+ * `LOG ` logs will not)
39
32
*/
40
33
export enum LogLevel {
41
34
DEBUG ,
42
- VERBOSE ,
35
+ LOG ,
43
36
INFO ,
44
37
WARN ,
45
38
ERROR ,
@@ -49,71 +42,67 @@ export enum LogLevel {
49
42
/**
50
43
* A container for the default log level
51
44
*/
52
- let defaultLogLevel : LogLevel = LogLevel . WARN ;
53
-
54
- /**
55
- * A function to set the default log level externally
56
- */
57
- export function setDefaultLogLevel ( val : LogLevel ) {
58
- if ( ! ( val in LogLevel ) ) {
59
- throw new TypeError ( 'Attempted to Invalid value assigned to `logLevel`' ) ;
60
- }
61
- defaultLogLevel = val ;
62
- }
45
+ const defaultLogLevel : LogLevel = LogLevel . WARN ;
63
46
64
47
/**
65
48
* We allow users the ability to pass their own log handler. We will pass the
66
49
* type of log, the current log level, and any other arguments passed (i.e. the
67
50
* messages that the user wants to log) to this function.
68
51
*/
69
52
export type LogHandler = (
53
+ loggerInstance : Logger ,
70
54
logType : LogLevel ,
71
- currentLogLevel : LogLevel ,
72
55
...args : any [ ]
73
56
) => void ;
74
57
75
58
/**
76
- * The default log handler will forward DEBUG, VERBOSE , INFO, WARN, and ERROR
59
+ * The default log handler will forward DEBUG, LOG , INFO, WARN, and ERROR
77
60
* messages on to their corresponding console counterparts (if the log method
78
61
* is supported by the current log level)
79
62
*/
80
63
const defaultLogHandler : LogHandler = (
81
- logType : LogLevel ,
82
- currentLevel : LogLevel ,
83
- ...args : any [ ]
64
+ instance ,
65
+ logType ,
66
+ ...args
84
67
) => {
85
- if ( logType < currentLevel ) return ;
68
+ if ( logType < instance . logLevel ) return ;
69
+ const now = new Date ( ) ;
86
70
switch ( logType ) {
87
71
case LogLevel . SILENT :
88
72
return ;
89
- case LogLevel . VERBOSE :
90
- console . log ( ...args ) ;
73
+ case LogLevel . LOG :
74
+ console . log ( `[ ${ now } ] ${ instance . name } :` , ...args ) ;
91
75
break ;
92
76
case LogLevel . INFO :
93
- console . info ( ...args ) ;
77
+ console . info ( `[ ${ now } ] ${ instance . name } :` , ...args ) ;
94
78
break ;
95
79
case LogLevel . WARN :
96
- console . warn ( ...args ) ;
80
+ console . warn ( `[ ${ now } ] ${ instance . name } :` , ...args ) ;
97
81
break ;
98
82
case LogLevel . ERROR :
99
- console . error ( ...args ) ;
83
+ console . error ( `[ ${ now } ] ${ instance . name } :` , ...args ) ;
100
84
break ;
101
85
default :
102
- console . debug ( ...args ) ;
86
+ console . log ( `[ ${ now } ] ${ instance . name } :` , ...args ) ;
103
87
}
104
88
} ;
105
89
106
90
export class Logger {
107
- constructor ( ) {
91
+ /**
92
+ * Gives you an instance of a Logger to capture messages according to
93
+ * Firebase's logging scheme.
94
+ *
95
+ * @param name The name that the logs will be associated with
96
+ */
97
+ constructor ( public name : string ) {
108
98
/**
109
99
* Capture the current instance for later use
110
100
*/
111
101
instances . push ( this ) ;
112
102
}
113
103
114
104
/**
115
- * The log level of the given logger. Though all of the log levels can be
116
- * centrally set, each logger can be set individually if it desires.
105
+ * The log level of the given Logger instance.
117
106
*/
118
107
private _logLevel = defaultLogLevel ;
119
108
get logLevel ( ) {
@@ -127,9 +116,7 @@ export class Logger {
127
116
}
128
117
129
118
/**
130
- * The log handler for the current logger instance. This can be set to any
131
- * function value, though this should not be needed the vast majority of the
132
- * time
119
+ * The log handler for the Logger instance.
133
120
*/
134
121
private _logHandler : LogHandler = defaultLogHandler ;
135
122
get logHandler ( ) {
@@ -147,18 +134,18 @@ export class Logger {
147
134
*/
148
135
149
136
debug ( ...args ) {
150
- this . _logHandler ( LogLevel . DEBUG , this . _logLevel , ...args ) ;
137
+ this . _logHandler ( this , LogLevel . DEBUG , ...args ) ;
151
138
}
152
139
log ( ...args ) {
153
- this . _logHandler ( LogLevel . VERBOSE , this . _logLevel , ...args ) ;
140
+ this . _logHandler ( this , LogLevel . LOG , ...args ) ;
154
141
}
155
142
info ( ...args ) {
156
- this . _logHandler ( LogLevel . INFO , this . _logLevel , ...args ) ;
143
+ this . _logHandler ( this , LogLevel . INFO , ...args ) ;
157
144
}
158
145
warn ( ...args ) {
159
- this . _logHandler ( LogLevel . WARN , this . _logLevel , ...args ) ;
146
+ this . _logHandler ( this , LogLevel . WARN , ...args ) ;
160
147
}
161
148
error ( ...args ) {
162
- this . _logHandler ( LogLevel . ERROR , this . _logLevel , ...args ) ;
149
+ this . _logHandler ( this , LogLevel . ERROR , ...args ) ;
163
150
}
164
151
}
0 commit comments