Skip to content

Commit a33b4b5

Browse files
committed
Refactor types, add doc comments.
1 parent 368834c commit a33b4b5

File tree

4 files changed

+96
-25
lines changed

4 files changed

+96
-25
lines changed

packages/app-types/index.d.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
import { LogCallback, LogLevelString, LogOptions } from '@firebase/logger';
1718

1819
export type FirebaseOptions = {
1920
apiKey?: string;
@@ -31,25 +32,6 @@ export interface FirebaseAppConfig {
3132
automaticDataCollectionEnabled?: boolean;
3233
}
3334

34-
export type LogLevelString =
35-
| 'debug'
36-
| 'verbose'
37-
| 'info'
38-
| 'warn'
39-
| 'error'
40-
| 'silent';
41-
42-
export interface LogOptions {
43-
level: LogLevelString;
44-
}
45-
46-
export type LogCallback = (callbackParams: {
47-
level: LogLevelString;
48-
message: string;
49-
args: unknown[];
50-
type: string;
51-
}) => void;
52-
5335
export class FirebaseApp {
5436
/**
5537
* The (read-only) name (identifier) for this App. '[DEFAULT]' is the default

packages/firebase/index.d.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ declare namespace firebase {
7878
complete: CompleteFn;
7979
}
8080

81+
/**
82+
* The JS SDK supports 5 log levels and also allows a user the ability to
83+
* silence the logs altogether.
84+
*
85+
* The order is as follows:
86+
* debug < verbose < info < warn < error
87+
*/
88+
type LogLevel = 'debug' | 'verbose' | 'info' | 'warn' | 'error' | 'silent';
89+
8190
/**
8291
* The current SDK version.
8392
*/
@@ -95,6 +104,49 @@ declare namespace firebase {
95104
variant?: string
96105
): void;
97106

107+
/**
108+
* Sets log level for all Firebase packages.
109+
*
110+
* All of the log types above the current log level will be captured (i.e. if
111+
* you set the log level to `info`, errors will still be logged, but `debug` and
112+
* `verbose` logs will not)
113+
*/
114+
function setLogLevel(logLevel: LogLevel): void;
115+
116+
/**
117+
* Sets log handler for all Firebase packages.
118+
* @param logCallback An optional custom log handler that will execute user code whenever
119+
* the Firebase SDK makes a logging call.
120+
*/
121+
function onLog(
122+
logCallback: (callbackParams: {
123+
/**
124+
* Level of event logged.
125+
*/
126+
level: LogLevel;
127+
/**
128+
* Any text from logged arguments joined into one string.
129+
*/
130+
message: string;
131+
/**
132+
* The raw arguments passed to the log call.
133+
*/
134+
args: unknown[];
135+
/**
136+
* A string indicating the source of the log call, usually a package name
137+
* such as `@firebase/firestore`.
138+
*/
139+
type: string;
140+
}) => void,
141+
options: {
142+
/**
143+
* Threshhold log level. Only logs at or above this level will trigger the `logCallback`
144+
* passed to `onLog`.
145+
*/
146+
level: LogLevel;
147+
}
148+
): void;
149+
98150
/**
99151
* @hidden
100152
*/

packages/logger/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ export {
2020
Logger,
2121
LogLevel,
2222
LogHandler,
23-
setUserLogHandler
23+
setUserLogHandler,
24+
LogCallback,
25+
LogLevelString,
26+
LogOptions
2427
} from './src/logger';

packages/logger/src/logger.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,24 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { LogCallback, LogLevelString, LogOptions } from '@firebase/app-types';
18+
export type LogLevelString =
19+
| 'debug'
20+
| 'verbose'
21+
| 'info'
22+
| 'warn'
23+
| 'error'
24+
| 'silent';
25+
26+
export interface LogOptions {
27+
level: LogLevelString;
28+
}
29+
30+
export type LogCallback = (callbackParams: {
31+
level: LogLevelString;
32+
message: string;
33+
args: unknown[];
34+
type: string;
35+
}) => void;
1936

2037
/**
2138
* A container for all of the Logger instances
@@ -196,9 +213,8 @@ export function setUserLogHandler(
196213
logCallback: LogCallback | null,
197214
options: LogOptions
198215
): void {
199-
if (typeof logCallback !== 'function') {
200-
console.warn('First argument to `onLog` must be a function.');
201-
return;
216+
if (logCallback !== null && typeof logCallback !== 'function') {
217+
throw new TypeError('First argument to `onLog` must be null or a function.');
202218
}
203219
for (const instance of instances) {
204220
let threshhold = instance.logLevel;
@@ -213,7 +229,25 @@ export function setUserLogHandler(
213229
level: LogLevel,
214230
...args: unknown[]
215231
) => {
216-
const message = args.map(arg => (arg as object).toString()).join(' ');
232+
const message = args.map(arg => {
233+
if (arg == null) {
234+
return null;
235+
} else if (typeof arg === 'string') {
236+
return arg;
237+
} else if (typeof arg === 'number' || typeof arg === 'boolean') {
238+
return arg.toString();
239+
} else if (arg instanceof Error) {
240+
return arg.message;
241+
} else {
242+
try {
243+
return JSON.stringify(arg);
244+
} catch(ignored) {
245+
return null;
246+
}
247+
}
248+
})
249+
.filter(arg => arg)
250+
.join(' ');
217251
if (level >= threshhold) {
218252
logCallback({
219253
level: LogLevel[level].toLowerCase() as LogLevelString,

0 commit comments

Comments
 (0)