Skip to content

Commit

Permalink
Suggesting array of categories and string union for LogCategory
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Mar 5, 2024
1 parent c3ffbe6 commit dc8fc50
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 72 deletions.
135 changes: 66 additions & 69 deletions packages/realm/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,75 +73,72 @@ export enum NumericLogLevel {
* @note
* When debugging, you might not need log messages from everything. To narrow
* this scope, log events can be grouped by category.
*
* `"Realm"`
* : Include logs from all categories.
*
* `"Realm.Storage"`
* : Database mutations and query operations.
*
* `"Realm.Storage.Transaction"`
* : Creating, advancing, and committing transactions.
*
* `"Realm.Storage.Query"`
* : Query operations.
*
* `"Realm.Storage.Object"`
* : Database mutations.
*
* `"Realm.Storage.Notification"`
* : Notifications of changes to the database.
*
* `"Realm.Sync"`
* : Activity related to Atlas Device Sync.
*
* `"Realm.Sync.Client"`
* : Activity related to Atlas Device Sync client operations.
*
* `"Realm.Sync.Client.Session"`
* : Connection level activity.
*
* `"Realm.Sync.Client.Changeset"`
* : Receiving, uploading, and integrating changesets.
*
* `"Realm.Sync.Client.Network"`
* : Low level network activity.
*
* `"Realm.Sync.Client.Reset"`
* : Client reset operations.
*
* `"Realm.Sync.Server"`
* : Activity related to Atlas Device Sync server operations.
*
* `"Realm.App"`
* : Log activity at the Atlas App level.
*
* `"Realm.SDK"`
* : Log activity at the SDK level.
*/
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",
}

export const LOG_CATEGORIES = [
"Realm",
"Realm.Storage",
"Realm.Storage.Transaction",
"Realm.Storage.Query",
"Realm.Storage.Object",
"Realm.Storage.Notification",
"Realm.Sync",
"Realm.Sync.Client",
"Realm.Sync.Client.Session",
"Realm.Sync.Client.Changeset",
"Realm.Sync.Client.Network",
"Realm.Sync.Client.Reset",
"Realm.Sync.Server",
"Realm.App",
"Realm.SDK",
] as const;

export type LogCategory = (typeof LOG_CATEGORIES)[number];

/**
* Log options to use when setting the log level.
Expand All @@ -154,7 +151,7 @@ export type LogOptions = {
level: LogLevel;
/**
* The category to set the log level for. If omitted, the log level
* is set for all categories ({@link LogCategory.Realm}).
* is set for all categories (`"Realm"`).
*/
category?: LogCategory;
};
Expand Down
7 changes: 4 additions & 3 deletions packages/realm/src/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
DefaultObject,
INTERNAL,
InitialSubscriptions,
LOG_CATEGORIES,
List,
LogCategory,
LogLevel,
Expand Down Expand Up @@ -126,13 +127,13 @@ export class Realm {
* @note The log level can be changed during the lifetime of the application.
* @since 12.7.0
* @example
* Realm.setLogLevel({ category: LogCategory.Realm, level: "all" });
* Realm.setLogLevel({ category: "Realm", level: "all" });
*/
static setLogLevel(options: LogOptions): void;

static setLogLevel(arg: LogLevel | LogOptions) {
const setLevel = (level: LogLevel, category = LogCategory.Realm) => {
assert(Object.values(LogCategory).includes(category), `Unexpected log category: '${category}'`);
const setLevel = (level: LogLevel, category = "Realm") => {
assert(LOG_CATEGORIES.includes(category as LogCategory), `Unexpected log category: '${category}'`);
const categoryRef = binding.LogCategoryRef.getCategory(category);
categoryRef.setDefaultLevelThreshold(toBindingLoggerLevel(level));
};
Expand Down

0 comments on commit dc8fc50

Please sign in to comment.