-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
257 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule realm-core
updated
448 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,18 +20,137 @@ import { assert, binding } from "./internal"; | |
|
||
export type LogLevel = "all" | "trace" | "debug" | "detail" | "info" | "warn" | "error" | "fatal" | "off"; | ||
|
||
/** | ||
* Log levels used by Realm | ||
*/ | ||
export enum NumericLogLevel { | ||
/** | ||
* Same as 'Trace' but with even more output. | ||
*/ | ||
All = 0, | ||
/** | ||
* A version of 'Debug' that allows for very high volume | ||
* output. | ||
*/ | ||
Trace = 1, | ||
/** | ||
* Reveal information that can aid debugging, no longer paying | ||
* attention to efficiency. | ||
*/ | ||
Debug = 2, | ||
/** | ||
* Same as 'Info', but prioritize completeness over minimalism. | ||
*/ | ||
Detail = 3, | ||
/** | ||
* Reveal information about what is going on, but in a | ||
* minimalistic fashion to avoid general overhead from logging | ||
* and to keep volume down. | ||
*/ | ||
Info = 4, | ||
/** | ||
* Be silent unless when there is an error or a warning. | ||
*/ | ||
Warn = 5, | ||
/** | ||
* Be silent unless when there is an error. | ||
*/ | ||
Error = 6, | ||
/** | ||
* Be silent unless when an error is fatal. | ||
*/ | ||
Fatal = 7, | ||
/** | ||
* Be silent. | ||
*/ | ||
Off = 8, | ||
} | ||
|
||
/** | ||
* The category to receive log messages for. The {@link LogLevel} will | ||
* always be set for a specific category. Setting the log level on one | ||
* category, will automatically set the log level for any subcategory. | ||
* @note | ||
* When debugging, you might not need log messages from everything. To narrow | ||
* this scope, log events can be grouped by category. | ||
*/ | ||
export enum LogCategory { | ||
/** | ||
* Include logs from all categories. Subcategories are {@link LogCategory.Storage}, | ||
* {@link LogCategory.Sync}, {@link LogCategory.App}, and {@link LogCategory.SDK}. | ||
*/ | ||
Realm = "Realm", | ||
/** | ||
* Log database mutations and query operations. | ||
* Subcategories are {@link LogCategory.Transaction}, {@link LogCategory.Object}, | ||
* {@link LogCategory.Query}, and {@link LogCategory.Notification}. | ||
*/ | ||
Storage = "Realm.Storage", | ||
/** | ||
* Log when creating, advancing, and committing transactions. | ||
*/ | ||
Transaction = "Realm.Storage.Transaction", | ||
/** | ||
* Log query operations. | ||
*/ | ||
Query = "Realm.Storage.Query", | ||
/** | ||
* Log database mutations. | ||
*/ | ||
Object = "Realm.Storage.Object", | ||
/** | ||
* Log notifications of changes to the database. | ||
*/ | ||
Notification = "Realm.Storage.Notification", | ||
/** | ||
* Log activity related to Atlas Device Sync. | ||
* Subcategories are {@link LogCategory.Client} and {@link LogCategory.Server}. | ||
*/ | ||
Sync = "Realm.Sync", | ||
/** | ||
* Log activity related to Atlas Device Sync client operations. | ||
* Subcategories are {@link LogCategory.Session}, {@link LogCategory.Changeset}, | ||
* {@link LogCategory.Network}, and {@link LogCategory.Reset}. | ||
*/ | ||
Client = "Realm.Sync.Client", | ||
/** | ||
* Log connection level activity. | ||
*/ | ||
Session = "Realm.Sync.Client.Session", | ||
/** | ||
* Log when receiving, uploading, and integrating changesets. | ||
*/ | ||
Changeset = "Realm.Sync.Client.Changeset", | ||
/** | ||
* Log low level network activity. | ||
*/ | ||
Network = "Realm.Sync.Client.Network", | ||
/** | ||
* Log client reset operations. | ||
*/ | ||
Reset = "Realm.Sync.Client.Reset", | ||
/** | ||
* Log activity related to Atlas Device Sync server operations. | ||
*/ | ||
Server = "Realm.Sync.Server", | ||
/** | ||
* Log activity at the Atlas App level. | ||
*/ | ||
App = "Realm.App", | ||
/** | ||
* Log activity at the SDK level. | ||
*/ | ||
SDK = "Realm.SDK", | ||
} | ||
|
||
/** | ||
* Type for `Realm.setLogLevel` | ||
*/ | ||
export type LogArgs = { | ||
level: LogLevel; | ||
category?: LogCategory; | ||
}; | ||
|
||
/** | ||
* A callback passed to `Realm.App.Sync.setLogger` when instrumenting the Atlas Device Sync client with a custom logger. | ||
* @param level - The level of the log entry between 0 and 8 inclusively. | ||
|
@@ -40,7 +159,30 @@ export enum NumericLogLevel { | |
*/ | ||
export type Logger = (level: NumericLogLevel, message: string) => void; | ||
|
||
export type LoggerCallback = (level: LogLevel, message: string) => void; | ||
/** | ||
* A callback passed to `Realm.setLogger`. | ||
* | ||
* @param level - The level of the log entry. | ||
* @param message - The message of the log entry. | ||
* @since 12.0.0 | ||
* @deprecated Will be removed in v13.0.0 | ||
*/ | ||
export type LoggerCallback1 = (level: LogLevel, message: string) => void; | ||
export type LoggerCallbackArgs = { | ||
category: LogCategory; | ||
level: LogLevel; | ||
message: string; | ||
}; | ||
/** | ||
* A callback passed to `Realm.setLogger`. Arguments are passed as a POJO. | ||
* | ||
* @param category - The category (origin) of the log entry. | ||
* @param level - The level of the log entry. | ||
* @param message - The message of the log entry. | ||
* @since | ||
Check warning on line 182 in packages/realm/src/Logger.ts
|
||
*/ | ||
export type LoggerCallback2 = (args: LoggerCallbackArgs) => void; | ||
export type LoggerCallback = LoggerCallback1 | LoggerCallback2; | ||
|
||
/** @internal */ | ||
export function toBindingLoggerLevel(arg: LogLevel): binding.LoggerLevel { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,12 @@ import { | |
INTERNAL, | ||
InitialSubscriptions, | ||
List, | ||
LogArgs, | ||
LogCategory, | ||
LogLevel, | ||
LoggerCallback, | ||
LoggerCallback1, | ||
LoggerCallback2, | ||
MigrationCallback, | ||
ObjectSchema, | ||
ProgressRealmPromise, | ||
|
@@ -109,24 +113,75 @@ export class Realm { | |
/** | ||
* Sets the log level. | ||
* @param level - The log level to be used by the logger. The default value is `info`. | ||
Check warning on line 115 in packages/realm/src/Realm.ts
|
||
* @param category - The category/component to set the log level for. If omitted, log level is set for all known categories. | ||
* @note The log level can be changed during the lifetime of the application. | ||
* @since 12.0.0 | ||
* @example | ||
* Realm.setLogLevel({ category: LogCategory.Realm, level: "all" }); | ||
*/ | ||
static setLogLevel(level: LogLevel) { | ||
const bindingLoggerLevel = toBindingLoggerLevel(level); | ||
binding.Logger.setDefaultLevelThreshold(bindingLoggerLevel); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
static setLogLevel(_: LogLevel | LogArgs) { | ||
// It is not possible to overload a static function: https://github.com/microsoft/TypeScript/issues/18945 | ||
|
||
const setLevel = (category: LogCategory, level: LogLevel) => { | ||
assert(Object.values(LogCategory).includes(category)); | ||
const ref = binding.LogCategoryRef; | ||
const c = ref.getCategory(category); | ||
c.setDefaultLevelThreshold(toBindingLoggerLevel(level)); | ||
}; | ||
|
||
// FIXME: don't use `arguments` but find a proper type | ||
if (arguments.length === 1) { | ||
// eslint-disable-next-line prefer-rest-params | ||
const arg = arguments[0]; | ||
if (arg.level) { | ||
const level = arg.level as LogLevel; | ||
if (arg.category) { | ||
const category = arg.category as LogCategory; | ||
setLevel(category, level); | ||
} else { | ||
Object.values(LogCategory).forEach((category) => { | ||
setLevel(category, level); | ||
}); | ||
} | ||
} else { | ||
const level = arg as LogLevel; | ||
Object.values(LogCategory).forEach((category) => { | ||
setLevel(category, level); | ||
}); | ||
} | ||
} else { | ||
throw new Error(`Wrong number of arguments - expected 1, got ${arguments.length}`); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the logger callback. | ||
* @param loggerCallback - The callback invoked by the logger. The default callback uses `console.log`, `console.warn` and `console.error`, depending on the level of the message. | ||
* @note The logger callback needs to be setup before opening the first realm. | ||
* @note The logger callback needs to be setup before opening the first Realm. | ||
* @since 12.0.0 | ||
* @example | ||
* Realm.setLogger(({ category, level, message }) => { | ||
* console.log(`[${category} - ${level}] ${message}`); | ||
* }); | ||
*/ | ||
static setLogger(loggerCallback: LoggerCallback) { | ||
const logger = binding.Helpers.makeLogger((level, message) => { | ||
loggerCallback(fromBindingLoggerLevelToLogLevel(level), message); | ||
}); | ||
let logger: binding.Logger; | ||
|
||
// This is a hack to check which of the two logger callbacks which are used | ||
// It only works as the two callback type have different number of arguments, and it will | ||
// probably produce odd error messages if the logger is set by `setLogger((...args) => console.log(args))`. | ||
if (loggerCallback.length === 2) { | ||
const cb = loggerCallback as LoggerCallback1; | ||
logger = binding.Helpers.makeLogger((_category, level, message) => { | ||
cb(fromBindingLoggerLevelToLogLevel(level), message); | ||
}); | ||
} else { | ||
const cb = loggerCallback as LoggerCallback2; | ||
logger = binding.Helpers.makeLogger((category, level, message) => { | ||
cb({ category: category as LogCategory, level: fromBindingLoggerLevelToLogLevel(level), message }); | ||
}); | ||
} | ||
binding.Logger.setDefaultLogger(logger); | ||
} | ||
|
||
|
Oops, something went wrong.