Skip to content

Commit 0c7f2fd

Browse files
committed
Address some PR comments
1 parent a0ca03c commit 0c7f2fd

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

packages/app/src/errors.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const enum AppError {
2222
BAD_APP_NAME = 'bad-app-name',
2323
DUPLICATE_APP = 'duplicate-app',
2424
APP_DELETED = 'app-deleted',
25-
INVALID_APP_ARGUMENT = 'invalid-app-argument'
25+
INVALID_APP_ARGUMENT = 'invalid-app-argument',
26+
INVALID_LOG_ARGUMENT = 'invalid-log-argument'
2627
}
2728

2829
const ERRORS: ErrorMap<AppError> = {
@@ -34,7 +35,8 @@ const ERRORS: ErrorMap<AppError> = {
3435
[AppError.APP_DELETED]: "Firebase App named '{$appName}' already deleted",
3536
[AppError.INVALID_APP_ARGUMENT]:
3637
'firebase.{$appName}() takes either no argument or a ' +
37-
'Firebase App instance.'
38+
'Firebase App instance.',
39+
[AppError.INVALID_LOG_ARGUMENT]: 'First argument to `onLog` must be null or a function.'
3840
};
3941

4042
type ErrorParams = { [key in AppError]: { appName: string } };

packages/app/src/firebaseNamespaceCore.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { FirebaseAppLiteImpl } from './lite/firebaseAppLite';
3434
import { DEFAULT_ENTRY_NAME, PLATFORM_LOG_STRING } from './constants';
3535
import { version } from '../../firebase/package.json';
3636
import { logger } from './logger';
37-
import { setUserLogHandler, setLogLevel } from '@firebase/logger';
37+
import { setUserLogHandler, setLogLevel, LogCallback, LogOptions } from '@firebase/logger';
3838
import { Component, ComponentType, Name } from '@firebase/component';
3939

4040
/**
@@ -62,7 +62,14 @@ export function createFirebaseNamespaceCore(
6262
app,
6363
registerVersion,
6464
setLogLevel,
65-
onLog: setUserLogHandler,
65+
onLog: (
66+
logCallback: LogCallback | null,
67+
options?: LogOptions) => {
68+
if (logCallback !== null && typeof logCallback !== 'function') {
69+
throw ERROR_FACTORY.create(AppError.INVALID_LOG_ARGUMENT, { appName: name });
70+
}
71+
setUserLogHandler(logCallback, options);
72+
},
6673
// @ts-ignore
6774
apps: null,
6875
SDK_VERSION: version,

packages/app/test/clientLogger.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe('User Log Methods', () => {
7878
});
7979

8080
it(`correctly triggers callback given to firebase.onLog()`, () => {
81+
// Note: default log level is INFO.
8182
firebase.initializeApp({});
8283
registerCoreComponents(firebase);
8384
(firebase as _FirebaseNamespace).INTERNAL.registerComponent(
@@ -91,6 +92,8 @@ describe('User Log Methods', () => {
9192
logger.info('hi');
9293
expect(result.level).to.equal('info');
9394
expect(result.message).to.equal('hi');
95+
expect(result.args).to.deep.equal(['hi']);
96+
expect(result.type).to.equal('@firebase/logger-test');
9497
expect(infoSpy.called).to.be.true;
9598
},
9699
ComponentType.PUBLIC

packages/logger/src/logger.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const ConsoleMethod = {
105105
* messages on to their corresponding console counterparts (if the log method
106106
* is supported by the current log level)
107107
*/
108-
const defaultLogHandler: LogHandler = (instance, logType, message): void => {
108+
const defaultLogHandler: LogHandler = (instance, logType, ...args): void => {
109109
if (logType < instance.logLevel) {
110110
return;
111111
}
@@ -114,7 +114,7 @@ const defaultLogHandler: LogHandler = (instance, logType, message): void => {
114114
if (method) {
115115
console[method as 'log' | 'info' | 'warn' | 'error'](
116116
`[${now}] ${instance.name}:`,
117-
message
117+
...args
118118
);
119119
} else {
120120
throw new Error(
@@ -215,11 +215,6 @@ export function setUserLogHandler(
215215
logCallback: LogCallback | null,
216216
options?: LogOptions
217217
): void {
218-
if (logCallback !== null && typeof logCallback !== 'function') {
219-
throw new TypeError(
220-
'First argument to `onLog` must be null or a function.'
221-
);
222-
}
223218
for (const instance of instances) {
224219
let threshhold = instance.logLevel;
225220
if (options && options.level) {

packages/logger/test/custom-logger.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe(`Custom log handler`, () => {
5151
});
5252

5353
it('calls custom callback with correct data for calling instance', () => {
54+
// Default log level is INFO.
5455
client1.info('info message!');
5556
expect(result.message).to.equal('info message!');
5657
expect(result.args[0]).to.equal('info message!');
@@ -67,6 +68,7 @@ describe(`Custom log handler`, () => {
6768
});
6869

6970
it('parses multiple arguments correctly', () => {
71+
// Default log level is INFO.
7072
client1.info('info message!', ['hello'], 1, { a: 3 });
7173
expect(result.message).to.equal('info message! ["hello"] 1 {"a":3}');
7274
expect(result.args).to.deep.equal(['info message!', ['hello'], 1, { a: 3 }]);
@@ -75,7 +77,8 @@ describe(`Custom log handler`, () => {
7577
});
7678

7779
it('calls custom callback when log call is above set log level', () => {
78-
// Info was already tested above.
80+
// Default log level is INFO.
81+
// INFO was already tested above.
7982
client1.warn('warning message!');
8083
expect(result.message).to.equal('warning message!');
8184
expect(result.level).to.equal('warn');
@@ -87,6 +90,7 @@ describe(`Custom log handler`, () => {
8790
});
8891

8992
it('does not call custom callback when log call is not above set log level', () => {
93+
// Default log level is INFO.
9094
client1.log('message you should not see');
9195
expect(result).to.be.null;
9296
expect(spies.logSpy.called).to.be.false;

0 commit comments

Comments
 (0)