Skip to content

Commit e9fed6d

Browse files
Simplify logging (#2806)
1 parent 228b047 commit e9fed6d

24 files changed

+167
-202
lines changed

packages/firestore/src/api/database.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ import {
6868
validateStringEnum,
6969
valueDescription
7070
} from '../util/input_validation';
71-
// eslint-disable-next-line import/no-duplicates
72-
import * as log from '../util/log';
73-
// eslint-disable-next-line import/no-duplicates
74-
import { LogLevel } from '../util/log';
71+
import { logError, setLogLevel, LogLevel, getLogLevel } from '../util/log';
7572
import { AutoId } from '../util/misc';
7673
import * as objUtils from '../util/obj';
7774
import { Deferred, Rejecter, Resolver } from '../util/promise';
@@ -198,13 +195,13 @@ class FirestoreSettings {
198195
// Nobody should set timestampsInSnapshots anymore, but the error depends on
199196
// whether they set it to true or false...
200197
if (settings.timestampsInSnapshots === true) {
201-
log.error(`
198+
logError(`
202199
The timestampsInSnapshots setting now defaults to true and you no
203200
longer need to explicitly set it. In a future release, the setting
204201
will be removed entirely and so it is recommended that you remove it
205202
from your firestore.settings() call now.`);
206203
} else if (settings.timestampsInSnapshots === false) {
207-
log.error(`
204+
logError(`
208205
The timestampsInSnapshots setting will soon be removed. YOU MUST UPDATE
209206
YOUR CODE.
210207
@@ -394,7 +391,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
394391

395392
if (settings) {
396393
if (settings.experimentalTabSynchronization !== undefined) {
397-
log.error(
394+
logError(
398395
"The 'experimentalTabSynchronization' setting has been renamed to " +
399396
"'synchronizeTabs'. In a future release, the setting will be removed " +
400397
'and it is recommended that you update your ' +
@@ -654,15 +651,14 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
654651
}
655652

656653
static get logLevel(): firestore.LogLevel {
657-
switch (log.getLogLevel()) {
654+
switch (getLogLevel()) {
658655
case LogLevel.DEBUG:
659656
return 'debug';
660-
case LogLevel.ERROR:
661-
return 'error';
662657
case LogLevel.SILENT:
663658
return 'silent';
664659
default:
665-
return fail('Unknown log level: ' + log.getLogLevel());
660+
// The default log level is error
661+
return 'error';
666662
}
667663
}
668664

@@ -671,13 +667,13 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
671667
validateArgType('Firestore.setLogLevel', 'non-empty string', 1, level);
672668
switch (level) {
673669
case 'debug':
674-
log.setLogLevel(log.LogLevel.DEBUG);
670+
setLogLevel(LogLevel.DEBUG);
675671
break;
676672
case 'error':
677-
log.setLogLevel(log.LogLevel.ERROR);
673+
setLogLevel(LogLevel.ERROR);
678674
break;
679675
case 'silent':
680-
log.setLogLevel(log.LogLevel.SILENT);
676+
setLogLevel(LogLevel.SILENT);
681677
break;
682678
default:
683679
throw new FirestoreError(

packages/firestore/src/api/user_data_writer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import * as firestore from '@firebase/firestore-types';
1919

2020
import * as api from '../protos/firestore_proto_api';
21-
import * as log from '../util/log';
2221

2322
import { DocumentReference, Firestore } from './database';
2423
import { Blob } from './blob';
@@ -41,6 +40,7 @@ import { forEach } from '../util/obj';
4140
import { TypeOrder } from '../model/field_value';
4241
import { ResourcePath } from '../model/path';
4342
import { isValidResourceName } from '../remote/serializer';
43+
import { logError } from '../util/log';
4444

4545
export type ServerTimestampBehavior = 'estimate' | 'previous' | 'none';
4646

@@ -139,7 +139,7 @@ export class UserDataWriter<T = firestore.DocumentData> {
139139

140140
if (!databaseId.isEqual(this.firestore._databaseId)) {
141141
// TODO(b/64130202): Somehow support foreign references.
142-
log.error(
142+
logError(
143143
`Document ${key} contains a document ` +
144144
`reference within a different database (` +
145145
`${databaseId.projectId}/${databaseId.database}) which is not ` +

packages/firestore/src/core/firestore_client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ import { Datastore } from '../remote/datastore';
3333
import { RemoteStore } from '../remote/remote_store';
3434
import { AsyncQueue } from '../util/async_queue';
3535
import { Code, FirestoreError } from '../util/error';
36-
import { debug } from '../util/log';
36+
import { logDebug } from '../util/log';
3737
import { Deferred } from '../util/promise';
3838
import {
3939
EventManager,
@@ -324,7 +324,7 @@ export class FirestoreClient {
324324
* implementation is available in this.persistence.
325325
*/
326326
private initializeRest(user: User): Promise<void> {
327-
debug(LOG_TAG, 'Initializing. user=', user.uid);
327+
logDebug(LOG_TAG, 'Initializing. user=', user.uid);
328328
return this.platform
329329
.loadConnection(this.databaseInfo)
330330
.then(async connection => {
@@ -406,7 +406,7 @@ export class FirestoreClient {
406406
private handleCredentialChange(user: User): Promise<void> {
407407
this.asyncQueue.verifyOperationInProgress();
408408

409-
debug(LOG_TAG, 'Credential Changed. Current user: ' + user.uid);
409+
logDebug(LOG_TAG, 'Credential Changed. Current user: ' + user.uid);
410410
return this.syncEngine.handleCredentialChange(user);
411411
}
412412

packages/firestore/src/core/sync_engine.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ import { RemoteStore } from '../remote/remote_store';
3434
import { RemoteSyncer } from '../remote/remote_syncer';
3535
import { assert, fail } from '../util/assert';
3636
import { Code, FirestoreError } from '../util/error';
37-
import * as log from '../util/log';
37+
import { logDebug } from '../util/log';
3838
import { primitiveComparator } from '../util/misc';
3939
import { ObjectMap } from '../util/obj_map';
4040
import { Deferred } from '../util/promise';
@@ -535,7 +535,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
535535
// had, we would have cached the affected documents), and so we will just
536536
// see any resulting document changes via normal remote document updates
537537
// as applicable.
538-
log.debug(LOG_TAG, 'Cannot apply mutation batch with id: ' + batchId);
538+
logDebug(LOG_TAG, 'Cannot apply mutation batch with id: ' + batchId);
539539
return;
540540
}
541541

@@ -611,7 +611,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
611611
*/
612612
async registerPendingWritesCallback(callback: Deferred<void>): Promise<void> {
613613
if (!this.remoteStore.canUseNetwork()) {
614-
log.debug(
614+
logDebug(
615615
LOG_TAG,
616616
'The network is disabled. The task returned by ' +
617617
"'awaitPendingWrites()' will not complete until the network is enabled."
@@ -751,7 +751,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
751751
this.limboDocumentRefs.addReference(limboChange.key, targetId);
752752
this.trackLimboChange(limboChange);
753753
} else if (limboChange instanceof RemovedLimboDocument) {
754-
log.debug(LOG_TAG, 'Document no longer in limbo: ' + limboChange.key);
754+
logDebug(LOG_TAG, 'Document no longer in limbo: ' + limboChange.key);
755755
this.limboDocumentRefs.removeReference(limboChange.key, targetId);
756756
const isReferenced = this.limboDocumentRefs.containsKey(
757757
limboChange.key
@@ -769,7 +769,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
769769
private trackLimboChange(limboChange: AddedLimboDocument): void {
770770
const key = limboChange.key;
771771
if (!this.limboTargetsByKey.get(key)) {
772-
log.debug(LOG_TAG, 'New document in limbo: ' + key);
772+
logDebug(LOG_TAG, 'New document in limbo: ' + key);
773773
const limboTargetId = this.limboTargetIdGenerator.next();
774774
const query = Query.atPath(key.path);
775775
this.limboResolutionsByTarget[limboTargetId] = new LimboResolution(key);
@@ -1047,7 +1047,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
10471047
if (this.isPrimary) {
10481048
// If we receive a target state notification via WebStorage, we are
10491049
// either already secondary or another tab has taken the primary lease.
1050-
log.debug(LOG_TAG, 'Ignoring unexpected query state notification.');
1050+
logDebug(LOG_TAG, 'Ignoring unexpected query state notification.');
10511051
return;
10521052
}
10531053

packages/firestore/src/local/index_free_query_engine.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2019 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@ import { QueryEngine } from './query_engine';
1919
import { LocalDocumentsView } from './local_documents_view';
2020
import { PersistenceTransaction } from './persistence';
2121
import { PersistencePromise } from './persistence_promise';
22-
import { Query, LimitType } from '../core/query';
22+
import { LimitType, Query } from '../core/query';
2323
import { SnapshotVersion } from '../core/snapshot_version';
2424
import {
2525
DocumentKeySet,
@@ -28,7 +28,7 @@ import {
2828
} from '../model/collections';
2929
import { Document } from '../model/document';
3030
import { assert } from '../util/assert';
31-
import { debug, getLogLevel, LogLevel } from '../util/log';
31+
import { getLogLevel, LogLevel, logDebug } from '../util/log';
3232
import { SortedSet } from '../util/sorted_set';
3333

3434
// TOOD(b/140938512): Drop SimpleQueryEngine and rename IndexFreeQueryEngine.
@@ -99,7 +99,7 @@ export class IndexFreeQueryEngine implements QueryEngine {
9999
}
100100

101101
if (getLogLevel() <= LogLevel.DEBUG) {
102-
debug(
102+
logDebug(
103103
'IndexFreeQueryEngine',
104104
'Re-using previous result from %s to execute query: %s',
105105
lastLimboFreeSnapshotVersion.toString(),
@@ -194,7 +194,7 @@ export class IndexFreeQueryEngine implements QueryEngine {
194194
query: Query
195195
): PersistencePromise<DocumentMap> {
196196
if (getLogLevel() <= LogLevel.DEBUG) {
197-
debug(
197+
logDebug(
198198
'IndexFreeQueryEngine',
199199
'Using full collection scan to execute query: %s',
200200
query.toString()

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { JsonProtoSerializer } from '../remote/serializer';
2626
import { assert, fail } from '../util/assert';
2727
import { AsyncQueue, TimerId } from '../util/async_queue';
2828
import { Code, FirestoreError } from '../util/error';
29-
import * as log from '../util/log';
29+
import { logDebug, logError } from '../util/log';
3030
import { CancelablePromise } from '../util/promise';
3131

3232
import { decode, encode, EncodedResourcePath } from './encoded_resource_path';
@@ -427,7 +427,7 @@ export class IndexedDbPersistence implements Persistence {
427427
throw e;
428428
}
429429

430-
log.debug(
430+
logDebug(
431431
LOG_TAG,
432432
'Releasing owner lease after error during lease refresh',
433433
e
@@ -634,7 +634,7 @@ export class IndexedDbPersistence implements Persistence {
634634
})
635635
.next(canActAsPrimary => {
636636
if (this.isPrimary !== canActAsPrimary) {
637-
log.debug(
637+
logDebug(
638638
LOG_TAG,
639639
`Client ${
640640
canActAsPrimary ? 'is' : 'is not'
@@ -760,7 +760,7 @@ export class IndexedDbPersistence implements Persistence {
760760
transaction: PersistenceTransaction
761761
) => PersistencePromise<T>
762762
): Promise<T> {
763-
log.debug(LOG_TAG, 'Starting transaction:', action);
763+
logDebug(LOG_TAG, 'Starting transaction:', action);
764764

765765
const simpleDbMode = mode === 'readonly' ? 'readonly' : 'readwrite';
766766

@@ -790,7 +790,7 @@ export class IndexedDbPersistence implements Persistence {
790790
})
791791
.next(holdsPrimaryLease => {
792792
if (!holdsPrimaryLease) {
793-
log.error(
793+
logError(
794794
`Failed to obtain primary lease for action '${action}'.`
795795
);
796796
this.isPrimary = false;
@@ -897,7 +897,7 @@ export class IndexedDbPersistence implements Persistence {
897897
const store = primaryClientStore(txn);
898898
return store.get(DbPrimaryClient.key).next(primaryClient => {
899899
if (this.isLocalClient(primaryClient)) {
900-
log.debug(LOG_TAG, 'Releasing primary lease.');
900+
logDebug(LOG_TAG, 'Releasing primary lease.');
901901
return store.delete(DbPrimaryClient.key);
902902
} else {
903903
return PersistencePromise.resolve();
@@ -913,7 +913,7 @@ export class IndexedDbPersistence implements Persistence {
913913
if (updateTimeMs < minAcceptable) {
914914
return false;
915915
} else if (updateTimeMs > maxAcceptable) {
916-
log.error(
916+
logError(
917917
`Detected an update time that is in the future: ${updateTimeMs} > ${maxAcceptable}`
918918
);
919919
return false;
@@ -1007,7 +1007,7 @@ export class IndexedDbPersistence implements Persistence {
10071007
const isZombied =
10081008
this.webStorage.getItem(this.zombiedClientLocalStorageKey(clientId)) !==
10091009
null;
1010-
log.debug(
1010+
logDebug(
10111011
LOG_TAG,
10121012
`Client '${clientId}' ${
10131013
isZombied ? 'is' : 'is not'
@@ -1016,7 +1016,7 @@ export class IndexedDbPersistence implements Persistence {
10161016
return isZombied;
10171017
} catch (e) {
10181018
// Gracefully handle if LocalStorage isn't working.
1019-
log.error(LOG_TAG, 'Failed to get zombied client id.', e);
1019+
logError(LOG_TAG, 'Failed to get zombied client id.', e);
10201020
return false;
10211021
}
10221022
}
@@ -1033,7 +1033,7 @@ export class IndexedDbPersistence implements Persistence {
10331033
);
10341034
} catch (e) {
10351035
// Gracefully handle if LocalStorage isn't available / working.
1036-
log.error('Failed to set zombie client id.', e);
1036+
logError('Failed to set zombie client id.', e);
10371037
}
10381038
}
10391039

packages/firestore/src/local/local_store.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ import {
3939
import { RemoteEvent, TargetChange } from '../remote/remote_event';
4040
import { assert } from '../util/assert';
4141
import { Code, FirestoreError } from '../util/error';
42-
import * as log from '../util/log';
42+
import { logDebug } from '../util/log';
4343
import { primitiveComparator } from '../util/misc';
4444
import * as objUtils from '../util/obj';
4545
import { ObjectMap } from '../util/obj_map';
@@ -610,7 +610,7 @@ export class LocalStore {
610610
documentBuffer.addEntry(doc, remoteVersion);
611611
changedDocs = changedDocs.insert(key, doc);
612612
} else {
613-
log.debug(
613+
logDebug(
614614
LOG_TAG,
615615
'Ignoring outdated watch update for ',
616616
key,
@@ -1119,7 +1119,7 @@ export async function ignoreIfPrimaryLeaseLoss(
11191119
err.code === Code.FAILED_PRECONDITION &&
11201120
err.message === PRIMARY_LEASE_LOST_ERROR_MSG
11211121
) {
1122-
log.debug(LOG_TAG, 'Unexpectedly lost primary lease');
1122+
logDebug(LOG_TAG, 'Unexpectedly lost primary lease');
11231123
} else {
11241124
throw err;
11251125
}

0 commit comments

Comments
 (0)