@adapty/capacitor
Version:
Official Adapty SDK for Capacitor
110 lines • 4.01 kB
JavaScript
import { LogLevel } from '../types/inputs';
import { consoleLogSink } from './console-sink';
import VERSION from '../../version';
export class Log {
// Formats a message for logging
static formatMessage(message, funcName) {
const now = new Date().toISOString();
const version = VERSION;
return `[${now}] [adapty@${version}] "${funcName}": ${message}`;
}
/** Configure JS logger: replace sinks and/or set default metadata */
static configure(config) {
var _a;
if (config.sinks) {
for (const sink of this.sinks)
(_a = sink.destroy) === null || _a === void 0 ? void 0 : _a.call(sink);
this.sinks = config.sinks.slice();
}
this.defaultMeta = config.defaultMeta;
}
/** Register additional sink */
static addSink(sink) {
this.sinks.push(sink);
}
/** Remove sink by id and call its destroy if present */
static removeSink(id) {
var _a;
const sink = this.sinks.find(sink => sink.id === id);
this.sinks = this.sinks.filter(sink => sink.id !== id);
(_a = sink === null || sink === void 0 ? void 0 : sink.destroy) === null || _a === void 0 ? void 0 : _a.call(sink);
}
/** Clear all sinks and destroy them */
static clearSinks() {
var _a;
const prev = this.sinks;
this.sinks = [];
for (const sink of prev)
(_a = sink.destroy) === null || _a === void 0 ? void 0 : _a.call(sink);
}
/**
* Gets the appropriate log level integer for a log level.
* @internal
*/
static getLogLevelInt(logLevel) {
switch (logLevel) {
case LogLevel.ERROR:
return 0;
case LogLevel.WARN:
return 1;
case LogLevel.INFO:
return 2;
case LogLevel.VERBOSE:
return 3;
}
}
/**
* Logs a message to the console if the log level is appropriate.
* Uses lazy evaluation to avoid unnecessary computations.
* @internal
*/
static log(logLevel, funcName, message, params) {
if (!Log.logLevel) {
return;
}
const currentLevel = Log.getLogLevelInt(Log.logLevel);
const messageLevel = Log.getLogLevelInt(logLevel);
if (messageLevel <= currentLevel) {
// Lazy evaluation: only compute once per entry
const resolvedMessage = message();
const resolvedParams = params ? params() : undefined;
const mergedParams = this.defaultMeta
? Object.assign(Object.assign({}, this.defaultMeta), (resolvedParams !== null && resolvedParams !== void 0 ? resolvedParams : {})) : resolvedParams;
const formatted = Log.formatMessage(resolvedMessage, funcName);
const event = {
timestamp: new Date().toISOString(),
version: String(VERSION),
level: logLevel,
funcName,
message: resolvedMessage,
params: mergedParams,
formatted,
};
for (const sink of this.sinks) {
if (sink.levels && !sink.levels.includes(logLevel))
continue;
try {
sink.handle(event);
}
catch (_a) {
// ignore sink errors
}
}
}
}
static info(funcName, message, params) {
this.log(LogLevel.INFO, funcName, message, params);
}
static warn(funcName, message, params) {
this.log(LogLevel.WARN, funcName, message, params);
}
static error(funcName, message, params) {
this.log(LogLevel.ERROR, funcName, message, params);
}
static verbose(funcName, message, params) {
this.log(LogLevel.VERBOSE, funcName, message, params);
}
}
Log.logLevel = null;
Log.sinks = [consoleLogSink];
//# sourceMappingURL=log.js.map