Skip to content

Commit

Permalink
Warn when stream is closing due to error, not debug. (#3064)
Browse files Browse the repository at this point in the history
* Warn when stream is closing due to error, not debug.

* Add setLogLevel

* Improvements.

* Add all levels.

* Add missing string values for log level
  • Loading branch information
wu-hui authored Jun 1, 2020
1 parent 9b32270 commit 31f7afc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
8 changes: 7 additions & 1 deletion packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ export interface PersistenceSettings {
experimentalTabSynchronization?: boolean;
}

export type LogLevel = 'debug' | 'error' | 'silent';
export type LogLevel =
| 'debug'
| 'error'
| 'silent'
| 'warn'
| 'info'
| 'verbose';

export function setLogLevel(logLevel: LogLevel): void;

Expand Down
32 changes: 15 additions & 17 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,16 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
switch (getLogLevel()) {
case LogLevel.DEBUG:
return 'debug';
case LogLevel.ERROR:
return 'error';
case LogLevel.SILENT:
return 'silent';
case LogLevel.WARN:
return 'warn';
case LogLevel.INFO:
return 'info';
case LogLevel.VERBOSE:
return 'verbose';
default:
// The default log level is error
return 'error';
Expand All @@ -630,23 +638,13 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {

static setLogLevel(level: firestore.LogLevel): void {
validateExactNumberOfArgs('Firestore.setLogLevel', arguments, 1);
validateArgType('Firestore.setLogLevel', 'non-empty string', 1, level);
switch (level) {
case 'debug':
setLogLevel(LogLevel.DEBUG);
break;
case 'error':
setLogLevel(LogLevel.ERROR);
break;
case 'silent':
setLogLevel(LogLevel.SILENT);
break;
default:
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid log level: ' + level
);
}
validateStringEnum(
'setLogLevel',
['debug', 'error', 'silent', 'warn', 'info', 'verbose'],
1,
level
);
setLogLevel(level);
}

// Note: this is not a property because the minifier can't work correctly with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
import { StreamBridge } from '../remote/stream_bridge';
import { debugAssert, fail, hardAssert } from '../util/assert';
import { Code, FirestoreError } from '../util/error';
import { logDebug } from '../util/log';
import { logDebug, logWarn } from '../util/log';
import { Indexable } from '../util/misc';
import { Rejecter, Resolver } from '../util/promise';
import { StringMap } from '../util/types';
Expand Down Expand Up @@ -352,7 +352,7 @@ export class WebChannelConnection implements Connection {
unguardedEventListen<Error>(WebChannel.EventType.ERROR, err => {
if (!closed) {
closed = true;
logDebug(LOG_TAG, 'WebChannel transport errored:', err);
logWarn(LOG_TAG, 'WebChannel transport errored:', err);
streamBridge.callOnClose(
new FirestoreError(
Code.UNAVAILABLE,
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/platform_node/grpc_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { mapCodeFromRpcCode } from '../remote/rpc_error';
import { StreamBridge } from '../remote/stream_bridge';
import { hardAssert } from '../util/assert';
import { FirestoreError } from '../util/error';
import { logError, logDebug } from '../util/log';
import { logError, logDebug, logWarn } from '../util/log';
import { NodeCallback, nodePromise } from '../util/node_api';
import { Deferred } from '../util/promise';

Expand Down Expand Up @@ -230,7 +230,7 @@ export class GrpcConnection implements Connection {
});

grpcStream.on('error', (grpcError: ServiceError) => {
logDebug(
logWarn(
LOG_TAG,
'GRPC stream error. Code:',
grpcError.code,
Expand Down
13 changes: 10 additions & 3 deletions packages/firestore/src/util/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { Logger, LogLevel } from '@firebase/logger';
import { Logger, LogLevel, LogLevelString } from '@firebase/logger';
import { SDK_VERSION } from '../core/version';
import { PlatformSupport } from '../platform/platform';

Expand All @@ -28,8 +28,8 @@ export function getLogLevel(): LogLevel {
return logClient.logLevel;
}

export function setLogLevel(newLevel: LogLevel): void {
logClient.logLevel = newLevel;
export function setLogLevel(newLevel: LogLevelString | LogLevel): void {
logClient.setLogLevel(newLevel);
}

export function logDebug(msg: string, ...obj: unknown[]): void {
Expand All @@ -46,6 +46,13 @@ export function logError(msg: string, ...obj: unknown[]): void {
}
}

export function logWarn(msg: string, ...obj: unknown[]): void {
if (logClient.logLevel <= LogLevel.WARN) {
const args = obj.map(argToString);
logClient.warn(`Firestore (${SDK_VERSION}): ${msg}`, ...args);
}
}

/**
* Converts an additional log parameter to a string representation.
*/
Expand Down

0 comments on commit 31f7afc

Please sign in to comment.