Skip to content

Warn when stream is closing due to error, not debug. #3064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Comment on lines +40 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning to make it public? If so, you have to update packages/firebase/index.d.ts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinged on chat


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 @@ -616,8 +616,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 @@ -626,23 +634,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably drop LogLevel as an input type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async_queue.test.ts calls getLogLevel() before changing it, then set the saved log level back when test is done. https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/test/unit/util/async_queue.test.ts#L84

It's only testing code, but i think it does no harm to keep it anyways?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM.

This could also be fixed by returning the LogLevelString from getLogLevel() and calling setLogLevel('silent') instead of setLogLevel(LogLevel.SILENT). We can leave as is though.

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