-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redesign the Logger API #8645
Closed
Closed
Redesign the Logger API #8645
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a503104
Don't create and then discard a dictionary for every message logged
tgoyne f4aa9e9
Restore tests for the deprecated logger functions
tgoyne 0936815
Rework the internal logging API and don't expose it
tgoyne ab72b7a
Redesign the Logger API
tgoyne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
|
||
#import <Realm/RLMConstants.h> | ||
|
||
@class RLMLoggerToken; | ||
|
||
RLM_HEADER_AUDIT_BEGIN(nullability) | ||
|
||
/// An enum representing different levels of sync-related logging that can be configured. | ||
|
@@ -75,7 +77,7 @@ typedef NS_ENUM(NSUInteger, RLMLogCategory) { | |
RLMLogCategoryRealm, | ||
/// Log category for all sdk related logs. | ||
RLMLogCategorySDK, | ||
/// Log category for all app related logs. | ||
/// Log category for all App related logs. | ||
RLMLogCategoryApp, | ||
/// Log category for all database related logs. | ||
RLMLogCategoryStorage, | ||
|
@@ -115,83 +117,134 @@ typedef void (^RLMLogFunction)(RLMLogLevel level, NSString *message); | |
/// The log function may be called from multiple threads simultaneously, and is | ||
/// responsible for performing its own synchronization if any is required. | ||
RLM_SWIFT_SENDABLE // invoked on a background thread | ||
typedef void (^RLMLogCategoryFunction)(RLMLogLevel level, RLMLogCategory category, NSString *message) NS_REFINED_FOR_SWIFT; | ||
typedef void (^RLMLogCategoryFunction)(RLMLogLevel level, RLMLogCategory category, NSString *message); | ||
|
||
/** | ||
Global logger class used by all Realm components. | ||
|
||
You can define your own logger creating an instance of `RLMLogger` and define the log function which will be | ||
invoked whenever there is a log message. | ||
Set this custom logger as you default logger using `setDefaultLogger`. | ||
By default messages are logged to NSLog(), with a log level of | ||
`RLMLogLevelInfo`. You can register an additional callback by calling | ||
`+[RLMLogger addLogFunction:]`: | ||
|
||
RLMLogger.defaultLogger = [[RLMLogger alloc] initWithLogFunction:^(RLMLogLevel level, NSString *category, NSString *message) { | ||
[RLMLogger addLogFunction:^(RLMLogLevel level, NSString *category, NSString *message) { | ||
NSLog(@"Realm Log - %lu, %@, %@", (unsigned long)level, category, message); | ||
}]; | ||
|
||
@note The default log threshold level is `RLMLogLevelInfo` for the log category `RLMLogCategoryRealm`, | ||
and logging strings are output to Apple System Logger. | ||
To remove the default NSLog-logger, call `[RLMLogger removeAll];` first. | ||
|
||
To change the log level, call `[RLMLogger setLevel:forCategory:]`. The | ||
`RLMLogCategoryRealm` will update all log categories at once. All log | ||
callbacks share the same log levels and are called for every category. | ||
*/ | ||
@interface RLMLogger : NSObject | ||
|
||
/// :nodoc: | ||
- (instancetype)init NS_UNAVAILABLE; | ||
|
||
#pragma mark Category-based API | ||
|
||
/** | ||
Gets the logging threshold level used by the logger. | ||
Registers a new logger callback function. | ||
|
||
The logger callback function will be invoked each time a message is logged | ||
with a log level greater than or equal to the current log level set for the | ||
message's category. The log function may be concurrently invoked from multiple | ||
threads. | ||
|
||
This function is thread-safe and can be called at any time, including from | ||
within other logger callbacks. It is guaranteed to work even if called | ||
concurrently with logging operations on another thread, but whether or not | ||
those operations are reported to the callback is left unspecified. | ||
|
||
This method returns a token which can be used to unregister the callback. | ||
Unlike notification tokens, storing this token is optional. If the token is | ||
destroyed without `invalidate` being called, it will be impossible to | ||
unregister the callback other than with `removeAll` or `resetToDefault`. | ||
*/ | ||
@property (nonatomic) RLMLogLevel level | ||
__attribute__((deprecated("Use `setLevel(level:category)` or `setLevel:category` instead."))); | ||
+ (RLMLoggerToken *)addLogFunction:(RLMLogCategoryFunction)function NS_REFINED_FOR_SWIFT; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like docstrings for the new APIs are missing parameters and returns details |
||
|
||
/// :nodoc: | ||
- (instancetype)init NS_UNAVAILABLE; | ||
/** | ||
Removes all registered callbacks. | ||
|
||
This function is thread-safe. If called concurrently with logging operations | ||
on other threads, the registered callbacks may be invoked one more time after | ||
this function returns. | ||
|
||
This is the only way to remove the default NSLog logging. | ||
*/ | ||
+ (void)removeAll; | ||
|
||
/** | ||
Creates a logger with the associated log level and the logic function to define your own logging logic. | ||
Resets all of the global logger state to the default. | ||
|
||
@param level The log level to be set for the logger. | ||
@param logFunction The log function which will be invoked whenever there is a log message. | ||
This removes all callbacks, adds the default NSLog callback, sets the log | ||
level to Info, and undoes the effects of calling `setDefaultLogger:`. | ||
*/ | ||
+ (void)resetToDefault; | ||
|
||
@note This will set the log level for the log category `RLMLogCategoryRealm`. | ||
/** | ||
Sets the log level for a given category. | ||
|
||
Some categories will also update the log level for child categories. See the | ||
documentation for RLMLogCategory for more details. | ||
*/ | ||
- (instancetype)initWithLevel:(RLMLogLevel)level logFunction:(RLMLogFunction)logFunction | ||
__attribute__((deprecated("Use `initWithLogFunction:` instead."))); | ||
+ (void)setLevel:(RLMLogLevel)level forCategory:(RLMLogCategory)category NS_REFINED_FOR_SWIFT; | ||
|
||
/** | ||
Creates a logger with a callback, which will be invoked whenever there is a log message. | ||
|
||
@param logFunction The log function which will be invoked whenever there is a log message. | ||
Gets the log level for the specified category. | ||
*/ | ||
- (instancetype)initWithLogFunction:(RLMLogCategoryFunction)logFunction; | ||
+ (RLMLogLevel)levelForCategory:(RLMLogCategory)category NS_REFINED_FOR_SWIFT; | ||
|
||
#pragma mark RLMLogger Default Logger API | ||
|
||
/** | ||
The current default logger. When setting a logger as default, this logger will replace the current default logger and will | ||
be used whenever information must be logged. | ||
*/ | ||
@property (class) RLMLogger *defaultLogger NS_SWIFT_NAME(shared); | ||
#pragma mark Deprecated API | ||
|
||
/** | ||
Log a message to the supplied level. | ||
|
||
@param logLevel The log level for the message. | ||
@param category The log category for the message. | ||
@param message The message to log. | ||
Gets the logging threshold level used by the logger. | ||
*/ | ||
- (void)logWithLevel:(RLMLogLevel)logLevel category:(RLMLogCategory)category message:(NSString *)message; | ||
@property (nonatomic) RLMLogLevel level | ||
__attribute__((deprecated("Use `setLevel(level:category)` or `setLevel:category` instead."))); | ||
|
||
/** | ||
Sets the gobal log level for a given category. | ||
Creates a logger with the associated log level and the logic function to define your own logging logic. | ||
|
||
@param level The log level to be set for the logger. | ||
@param category The log function which will be invoked whenever there is a log message. | ||
@param logFunction The log function which will be invoked whenever there is a log message. | ||
|
||
@note This will set the log level for the log category `RLMLogCategoryRealm`. | ||
*/ | ||
+ (void)setLevel:(RLMLogLevel)level forCategory:(RLMLogCategory)category NS_REFINED_FOR_SWIFT; | ||
- (instancetype)initWithLevel:(RLMLogLevel)level logFunction:(RLMLogFunction)logFunction | ||
__attribute__((deprecated("Use `+[Logger addLogFunction:]` instead."))); | ||
|
||
/** | ||
Gets the global log level for the specified category. | ||
The current default logger. When setting a logger as default, this logger will | ||
replace the current default logger and will be used whenever information must | ||
be logged. | ||
|
||
@param category The log category which we need the level. | ||
@returns The log level for the specified category | ||
*/ | ||
+ (RLMLogLevel)levelForCategory:(RLMLogCategory)category NS_REFINED_FOR_SWIFT; | ||
Overriding the default logger will result in callbacks registered with | ||
`addLogFunction:` never being invoked. | ||
*/ | ||
@property (class) RLMLogger *defaultLogger NS_SWIFT_NAME(shared) | ||
__attribute__((deprecated("Use `+[Logger addLogFunction:]` instead."))); | ||
@end | ||
|
||
/** | ||
A token which can be used to remove logger callbacks. | ||
|
||
This token only needs to be stored if you wish to be able to remove individual | ||
callbacks. If the token is destroyed without `invalidate` being called the | ||
callback will not be removed. | ||
*/ | ||
RLM_SWIFT_SENDABLE RLM_FINAL | ||
@interface RLMLoggerToken : NSObject | ||
/** | ||
Removes the associated logger callback. | ||
|
||
This function is thread-safe and idempotent. Calling it multiple times or from | ||
multiple threads at once is not an error. If called concurrently with logging | ||
operations on another thread, the associated callback may be called one more | ||
time per thread after this function returns. | ||
*/ | ||
- (void)invalidate; | ||
@end | ||
|
||
RLM_HEADER_AUDIT_END(nullability) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To remove the default NSLog-logger, call
[RLMLogger removeAll];
first.To change the log level, call
[RLMLogger setLevel:forCategory:]
.The
RLMLogCategoryRealm
will update all log categories at once. All logcallbacks share the same log levels and are called for every category.