Skip to content

Commit 81ae912

Browse files
committed
initial log work
1 parent 33388ae commit 81ae912

File tree

6 files changed

+156
-48
lines changed

6 files changed

+156
-48
lines changed

packages/analytics/src/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
GTAG_URL
3232
} from './constants';
3333
import { FirebaseInstallations } from '@firebase/installations-types';
34+
import { logger } from './logger';
3435

3536
/**
3637
* Initialize the analytics instance in gtag.js by calling config command with fid.
@@ -146,7 +147,7 @@ function wrapGtag(
146147
gtagParams || {}
147148
)
148149
)
149-
.catch(e => console.error(e));
150+
.catch(e => logger.error(e));
150151
} else if (command === GtagCommand.CONFIG) {
151152
const initializationPromiseToWait =
152153
initializedIdPromisesMap[idOrNameOrParams as string] ||
@@ -155,7 +156,7 @@ function wrapGtag(
155156
.then(() => {
156157
gtagCore(GtagCommand.CONFIG, idOrNameOrParams as string, gtagParams);
157158
})
158-
.catch(e => console.error(e));
159+
.catch(e => logger.error(e));
159160
} else {
160161
// SET command.
161162
// Splitting calls for CONFIG and SET to make it clear which signature

packages/analytics/src/logger.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { Logger } from '@firebase/logger';
19+
20+
export const logger = new Logger('@firebase/analytics');

packages/app-types/index.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { FirebaseService } from './private';
2+
13
/**
24
* @license
35
* Copyright 2017 Google Inc.
@@ -31,6 +33,25 @@ export interface FirebaseAppConfig {
3133
automaticDataCollectionEnabled?: boolean;
3234
}
3335

36+
export type LogLevelString =
37+
| 'debug'
38+
| 'verbose'
39+
| 'info'
40+
| 'warn'
41+
| 'error'
42+
| 'silent';
43+
44+
export interface LogOptions {
45+
level: LogLevelString;
46+
}
47+
48+
export type LogCallback = (callbackParams: {
49+
level: LogLevelString;
50+
message: string;
51+
source: FirebaseService | FirebaseApp;
52+
type: string;
53+
}) => void;
54+
3455
export class FirebaseApp {
3556
/**
3657
* The (read-only) name (identifier) for this App. '[DEFAULT]' is the default
@@ -105,6 +126,12 @@ export interface FirebaseNamespace {
105126
*/
106127
registerVersion(library: string, version: string, variant?: string): void;
107128

129+
// Sets log level for all Firebase components.
130+
setLogLevel(logLevel: LogLevelString): void;
131+
132+
// Sets log handler for all Firebase components.
133+
onLog(logCallback: LogCallback, options: LogOptions): void;
134+
108135
// The current SDK version.
109136
SDK_VERSION: string;
110137
}

packages/app/src/firebaseNamespaceCore.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +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 { addLogCallback, setLogLevel } from '@firebase/logger';
3738
import { Component, ComponentType, Name } from '@firebase/component';
3839

3940
/**
@@ -60,6 +61,8 @@ export function createFirebaseNamespaceCore(
6061
// @ts-ignore
6162
app,
6263
registerVersion,
64+
setLogLevel,
65+
onLog: addLogCallback,
6366
// @ts-ignore
6467
apps: null,
6568
SDK_VERSION: version,

packages/logger/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,4 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { instances, LogLevel } from './src/logger';
19-
20-
export function setLogLevel(level: LogLevel): void {
21-
instances.forEach(inst => {
22-
inst.logLevel = level;
23-
});
24-
}
25-
26-
export { Logger, LogLevel, LogHandler } from './src/logger';
18+
export { setLogLevel, Logger, LogLevel, LogHandler, addLogCallback } from './src/logger';

packages/logger/src/logger.ts

Lines changed: 102 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18+
import {
19+
LogCallback,
20+
LogLevelString,
21+
FirebaseApp,
22+
LogOptions
23+
} from '@firebase/app-types';
24+
import { FirebaseService } from '@firebase/app-types/private';
25+
1826
/**
1927
* A container for all of the Logger instances
2028
*/
@@ -40,6 +48,15 @@ export enum LogLevel {
4048
SILENT
4149
}
4250

51+
const levelStringToEnum = {
52+
'debug': LogLevel.DEBUG,
53+
'verbose': LogLevel.VERBOSE,
54+
'info': LogLevel.INFO,
55+
'warn': LogLevel.WARN,
56+
'error': LogLevel.ERROR,
57+
'silent': LogLevel.SILENT
58+
};
59+
4360
/**
4461
* The default log level
4562
*/
@@ -53,45 +70,45 @@ const defaultLogLevel: LogLevel = LogLevel.INFO;
5370
export type LogHandler = (
5471
loggerInstance: Logger,
5572
logType: LogLevel,
73+
message: string,
74+
source?: FirebaseService | FirebaseApp,
5675
...args: unknown[]
5776
) => void;
5877

78+
/**
79+
* By default, `console.debug` is not displayed in the developer console (in
80+
* chrome). To avoid forcing users to have to opt-in to these logs twice
81+
* (i.e. once for firebase, and once in the console), we are sending `DEBUG`
82+
* logs to the `console.log` function.
83+
*/
84+
const ConsoleMethod = {
85+
[LogLevel.DEBUG]: 'log',
86+
[LogLevel.VERBOSE]: 'log',
87+
[LogLevel.INFO]: 'info',
88+
[LogLevel.WARN]: 'warn',
89+
[LogLevel.ERROR]: 'error'
90+
};
91+
5992
/**
6093
* The default log handler will forward DEBUG, VERBOSE, INFO, WARN, and ERROR
6194
* messages on to their corresponding console counterparts (if the log method
6295
* is supported by the current log level)
6396
*/
64-
const defaultLogHandler: LogHandler = (instance, logType, ...args): void => {
97+
const defaultLogHandler: LogHandler = (instance, logType, message): void => {
6598
if (logType < instance.logLevel) {
6699
return;
67100
}
68101
const now = new Date().toISOString();
69-
switch (logType) {
70-
/**
71-
* By default, `console.debug` is not displayed in the developer console (in
72-
* chrome). To avoid forcing users to have to opt-in to these logs twice
73-
* (i.e. once for firebase, and once in the console), we are sending `DEBUG`
74-
* logs to the `console.log` function.
75-
*/
76-
case LogLevel.DEBUG:
77-
console.log(`[${now}] ${instance.name}:`, ...args);
78-
break;
79-
case LogLevel.VERBOSE:
80-
console.log(`[${now}] ${instance.name}:`, ...args);
81-
break;
82-
case LogLevel.INFO:
83-
console.info(`[${now}] ${instance.name}:`, ...args);
84-
break;
85-
case LogLevel.WARN:
86-
console.warn(`[${now}] ${instance.name}:`, ...args);
87-
break;
88-
case LogLevel.ERROR:
89-
console.error(`[${now}] ${instance.name}:`, ...args);
90-
break;
91-
default:
92-
throw new Error(
93-
`Attempted to log a message with an invalid logType (value: ${logType})`
94-
);
102+
const method = ConsoleMethod[logType as keyof typeof ConsoleMethod];
103+
if (method) {
104+
console[method as 'log' | 'info' | 'warn' | 'error'](
105+
`[${now}] ${instance.name}:`,
106+
message
107+
);
108+
} else {
109+
throw new Error(
110+
`Attempted to log a message with an invalid logType (value: ${logType})`
111+
);
95112
}
96113
};
97114

@@ -141,19 +158,67 @@ export class Logger {
141158
* The functions below are all based on the `console` interface
142159
*/
143160

144-
debug(...args: unknown[]): void {
145-
this._logHandler(this, LogLevel.DEBUG, ...args);
161+
debug(
162+
message: string,
163+
source?: FirebaseService | FirebaseApp
164+
): void {
165+
this._logHandler(this, LogLevel.DEBUG, message, source);
166+
}
167+
log(
168+
message: string,
169+
source?: FirebaseService | FirebaseApp
170+
): void {
171+
this._logHandler(this, LogLevel.VERBOSE, message, source);
146172
}
147-
log(...args: unknown[]): void {
148-
this._logHandler(this, LogLevel.VERBOSE, ...args);
173+
info(
174+
message: string,
175+
source?: FirebaseService | FirebaseApp
176+
): void {
177+
this._logHandler(this, LogLevel.INFO, message, source);
149178
}
150-
info(...args: unknown[]): void {
151-
this._logHandler(this, LogLevel.INFO, ...args);
179+
warn(
180+
message: string,
181+
source?: FirebaseService | FirebaseApp
182+
): void {
183+
this._logHandler(this, LogLevel.WARN, message, source);
152184
}
153-
warn(...args: unknown[]): void {
154-
this._logHandler(this, LogLevel.WARN, ...args);
185+
error(
186+
message: string,
187+
source?: FirebaseService | FirebaseApp
188+
): void {
189+
this._logHandler(this, LogLevel.ERROR, message, source);
155190
}
156-
error(...args: unknown[]): void {
157-
this._logHandler(this, LogLevel.ERROR, ...args);
191+
}
192+
193+
export function setLogLevel(level: LogLevelString | LogLevel): void {
194+
const newLevel = typeof level === 'string' ? levelStringToEnum[level] : level;
195+
instances.forEach(inst => {
196+
inst.logLevel = newLevel;
197+
});
198+
}
199+
200+
export function addLogCallback(logCallback: LogCallback, options: LogOptions) {
201+
for (const index in instances) {
202+
const instance = instances[index];
203+
let threshhold = instance.logLevel;
204+
if (options && options.level) {
205+
threshhold = levelStringToEnum[options.level];
206+
}
207+
instance.logHandler = (
208+
instance: Logger,
209+
level: LogLevel,
210+
message: string,
211+
source?: FirebaseService | FirebaseApp
212+
) => {
213+
if (level >= threshhold && message && source) {
214+
logCallback({
215+
level: LogLevel[level].toLowerCase() as LogLevelString,
216+
message,
217+
type: instance.name,
218+
source
219+
});
220+
}
221+
defaultLogHandler(instance, level, message);
222+
};
158223
}
159224
}

0 commit comments

Comments
 (0)