1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-12-24 01:43:18 +00:00
musix-oss/node_modules/@firebase/logger/dist/index.esm2017.js
2020-03-03 22:30:50 +02:00

165 lines
5.2 KiB
JavaScript

/**
* @license
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A container for all of the Logger instances
*/
const instances = [];
/**
* The JS SDK supports 5 log levels and also allows a user the ability to
* silence the logs altogether.
*
* The order is a follows:
* DEBUG < VERBOSE < INFO < WARN < ERROR
*
* All of the log types above the current log level will be captured (i.e. if
* you set the log level to `INFO`, errors will still be logged, but `DEBUG` and
* `VERBOSE` logs will not)
*/
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
LogLevel[LogLevel["VERBOSE"] = 1] = "VERBOSE";
LogLevel[LogLevel["INFO"] = 2] = "INFO";
LogLevel[LogLevel["WARN"] = 3] = "WARN";
LogLevel[LogLevel["ERROR"] = 4] = "ERROR";
LogLevel[LogLevel["SILENT"] = 5] = "SILENT";
})(LogLevel || (LogLevel = {}));
/**
* The default log level
*/
const defaultLogLevel = LogLevel.INFO;
/**
* The default log handler will forward DEBUG, VERBOSE, INFO, WARN, and ERROR
* messages on to their corresponding console counterparts (if the log method
* is supported by the current log level)
*/
const defaultLogHandler = (instance, logType, ...args) => {
if (logType < instance.logLevel) {
return;
}
const now = new Date().toISOString();
switch (logType) {
/**
* By default, `console.debug` is not displayed in the developer console (in
* chrome). To avoid forcing users to have to opt-in to these logs twice
* (i.e. once for firebase, and once in the console), we are sending `DEBUG`
* logs to the `console.log` function.
*/
case LogLevel.DEBUG:
console.log(`[${now}] ${instance.name}:`, ...args);
break;
case LogLevel.VERBOSE:
console.log(`[${now}] ${instance.name}:`, ...args);
break;
case LogLevel.INFO:
console.info(`[${now}] ${instance.name}:`, ...args);
break;
case LogLevel.WARN:
console.warn(`[${now}] ${instance.name}:`, ...args);
break;
case LogLevel.ERROR:
console.error(`[${now}] ${instance.name}:`, ...args);
break;
default:
throw new Error(`Attempted to log a message with an invalid logType (value: ${logType})`);
}
};
class Logger {
/**
* Gives you an instance of a Logger to capture messages according to
* Firebase's logging scheme.
*
* @param name The name that the logs will be associated with
*/
constructor(name) {
this.name = name;
/**
* The log level of the given Logger instance.
*/
this._logLevel = defaultLogLevel;
/**
* The log handler for the Logger instance.
*/
this._logHandler = defaultLogHandler;
/**
* Capture the current instance for later use
*/
instances.push(this);
}
get logLevel() {
return this._logLevel;
}
set logLevel(val) {
if (!(val in LogLevel)) {
throw new TypeError('Invalid value assigned to `logLevel`');
}
this._logLevel = val;
}
get logHandler() {
return this._logHandler;
}
set logHandler(val) {
if (typeof val !== 'function') {
throw new TypeError('Value assigned to `logHandler` must be a function');
}
this._logHandler = val;
}
/**
* The functions below are all based on the `console` interface
*/
debug(...args) {
this._logHandler(this, LogLevel.DEBUG, ...args);
}
log(...args) {
this._logHandler(this, LogLevel.VERBOSE, ...args);
}
info(...args) {
this._logHandler(this, LogLevel.INFO, ...args);
}
warn(...args) {
this._logHandler(this, LogLevel.WARN, ...args);
}
error(...args) {
this._logHandler(this, LogLevel.ERROR, ...args);
}
}
/**
* @license
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function setLogLevel(level) {
instances.forEach(inst => {
inst.logLevel = level;
});
}
export { LogLevel, Logger, setLogLevel };
//# sourceMappingURL=index.esm2017.js.map