Skip to content

Commit 1d027ed

Browse files
committed
Address @mikelehen feedback
1 parent 2ba4043 commit 1d027ed

File tree

6 files changed

+38
-31
lines changed

6 files changed

+38
-31
lines changed

packages/firestore/src/util/log.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,37 @@ export function getLogLevel(): LogLevel {
4141
}
4242
export function setLogLevel(newLevel: LogLevel): void {
4343
/**
44-
* Since Firestore's LogLevel enum is a pure subset of Firebase's LogLevel, we
45-
* can return it directly.
44+
* Map the new log level to the associated Firebase Log Level
4645
*/
47-
logClient.logLevel = newLevel;
46+
switch (newLevel) {
47+
case LogLevel.DEBUG:
48+
logClient.logLevel = FirebaseLogLevel.DEBUG;
49+
break;
50+
case LogLevel.ERROR:
51+
logClient.logLevel = FirebaseLogLevel.ERROR;
52+
break;
53+
case LogLevel.SILENT:
54+
logClient.logLevel = FirebaseLogLevel.SILENT;
55+
break;
56+
default:
57+
logClient.error(`Firestore (${SDK_VERSION}): Invalid value passed to \`setLogLevel\``);
58+
}
4859
}
4960

5061
export function debug(tag: string, msg: string, ...obj: AnyJs[]): void {
5162
if (logClient.logLevel <= FirebaseLogLevel.DEBUG) {
52-
const time = new Date().toISOString();
5363
const args = obj.map(argToString);
5464
logClient.log(
55-
`Firestore (${SDK_VERSION}) ${time} [${tag}]: ${msg}`,
65+
`Firestore (${SDK_VERSION}) [${tag}]: ${msg}`,
5666
...args
5767
);
5868
}
5969
}
6070

6171
export function error(msg: string, ...obj: AnyJs[]): void {
6272
if (logClient.logLevel <= FirebaseLogLevel.ERROR) {
63-
const time = new Date().toISOString();
6473
const args = obj.map(argToString);
65-
logClient.error(`Firestore (${SDK_VERSION}) ${time}: ${msg}`, ...args);
74+
logClient.error(`Firestore (${SDK_VERSION}): ${msg}`, ...args);
6675
}
6776
}
6877

packages/logger/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ instance:
3434
Each log will be formatted in the following manner:
3535

3636
```typescript
37-
`[${new Date()}] ${this.name}: ${...args}`
37+
`[${new Date()}] ${COMPONENT_NAME}: ${...args}`
3838
```
3939

packages/logger/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

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-
2417
import { instances, LogLevel } from './src/logger';
2518

2619
export function setLogLevel(level: LogLevel) {

packages/logger/src/logger.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export enum LogLevel {
4040
}
4141

4242
/**
43-
* A container for the default log level
43+
* The sefault log level
4444
*/
4545
const defaultLogLevel: LogLevel = LogLevel.WARN;
4646

@@ -62,10 +62,25 @@ export type LogHandler = (
6262
*/
6363
const defaultLogHandler: LogHandler = (instance, logType, ...args) => {
6464
if (logType < instance.logLevel) return;
65-
const now = new Date();
65+
const now = new Date().toISOString();
6666
switch (logType) {
67+
/**
68+
* The default log handler doesn't do anything with LogLevel silent, so we
69+
* are early returning here. To allow custom log handlers to handle this
70+
* behavior differently, we are still going to pass through logging calls
71+
* to their handlers when the log level is set to `SILENT`.
72+
*/
6773
case LogLevel.SILENT:
6874
return;
75+
/**
76+
* By default, `console.debug` is not displayed in the developer console (in
77+
* chrome). To avoid forcing users to have to opt-in to these logs twice
78+
* (i.e. once for firebase, and once in the console), we are sending `DEBUG`
79+
* logs to the `console.log` function.
80+
*/
81+
case LogLevel.DEBUG:
82+
console.log(`[${now}] ${instance.name}:`, ...args);
83+
break;
6984
case LogLevel.LOG:
7085
console.log(`[${now}] ${instance.name}:`, ...args);
7186
break;
@@ -79,7 +94,7 @@ const defaultLogHandler: LogHandler = (instance, logType, ...args) => {
7994
console.error(`[${now}] ${instance.name}:`, ...args);
8095
break;
8196
default:
82-
console.log(`[${now}] ${instance.name}:`, ...args);
97+
throw new Error(`Attempted to log a message with an invalid logType (value: ${logType})`);
8398
}
8499
};
85100

packages/logger/test/logger.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ describe('@firebase/logger', () => {
2424
const message = 'Hello there!';
2525
let client: Logger;
2626
const spies = {
27-
debugSpy: null,
2827
logSpy: null,
2928
infoSpy: null,
3029
warnSpy: null,
@@ -52,7 +51,8 @@ describe('@firebase/logger', () => {
5251

5352
function testLog(message, channel, shouldLog) {
5453
/**
55-
* `debug` logs also go through log channel
54+
* Ensure that `debug` logs assert against the `console.log` function. The
55+
* rationale here is explained in `logger.ts`.
5656
*/
5757
channel = channel === 'debug' ? 'log' : channel;
5858

@@ -81,10 +81,7 @@ describe('@firebase/logger', () => {
8181
testLog(message, 'error', true);
8282
});
8383

84-
describe('Defaults', () => {
85-
beforeEach(() => {
86-
setLogLevel(LogLevel.WARN);
87-
});
84+
describe('Defaults to LogLevel.WARN', () => {
8885
testLog(message, 'debug', false);
8986
testLog(message, 'log', false);
9087
testLog(message, 'info', false);

packages/template/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

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-
2417
import { testFxn } from './src';
2518

2619
testFxn();

0 commit comments

Comments
 (0)