Skip to content

Commit f501ed6

Browse files
LeoNatanfacebook-github-bot
authored andcommitted
Use Apple unified logging API (os_log) (#27892)
Summary: As discussed in #27863, the following changes were made to modernize the internal default logging function: - `RCTDefaultLogThreshold` is now set to `RCTLogLevelTrace` in both release and debug builds—the Apple logging system will discard uncollected log entires, while allowing for collection when needed - `RCTLogLevel` is translated to the appropriate log type - The log subsystem is "com.facebook.react.log" - `RCTLogSource` translates to the appropriate category ("native"/"javascript") - Log the provided message using `os_log_with_type` Closes #27863 ## Changelog [iOS] [Changed] - Use Apple unified logging API (os_log) Pull Request resolved: #27892 Test Plan: ## From Original PR Ran a test app in the iOS simulator, and verified that logs are correctly displayed in Console.app as well as using the following command: ```sh /usr/bin/xcrun simctl spawn booted log stream --level debug --style compact --predicate 'process=="ReactNativeTesterApp" && subsystem=="com.facebook.react.log"' ``` ## Peter's Test Plan 1. Apply P125583473 2. Verify log output in Xcode P125583504 3. Apply this diff 4. Verify log output in Xcode P125583597 These appear unchanged, after digging into why, I realized that FB doesn't even use the default log function, we inject a custom one [here](https://fburl.com/diffusion/887a1axs). So this PR shouldn't affect us at all. :) Differential Revision: D19605414 Pulled By: PeteTheHeat fbshipit-source-id: 1d70fb702c337a759905d4a65a951a31353ce775
1 parent 9cc920b commit f501ed6

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

React/Base/RCTLog.mm

+39-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cxxabi.h>
1111

1212
#import <objc/message.h>
13+
#import <os/log.h>
1314

1415
#import "RCTRedBoxSetEnabled.h"
1516
#import "RCTAssert.h"
@@ -28,11 +29,8 @@
2829
"fatal",
2930
};
3031

31-
#if RCT_DEBUG
32+
/* os log will discard debug and info messages if they are not needed */
3233
static const RCTLogLevel RCTDefaultLogThreshold = (RCTLogLevel)(RCTLogLevelInfo - 1);
33-
#else
34-
static const RCTLogLevel RCTDefaultLogThreshold = RCTLogLevelError;
35-
#endif
3634

3735
static RCTLogFunction RCTCurrentLogFunction;
3836
static RCTLogLevel RCTCurrentLogThreshold = RCTDefaultLogThreshold;
@@ -46,17 +44,48 @@ void RCTSetLogThreshold(RCTLogLevel threshold) {
4644
RCTCurrentLogThreshold = threshold;
4745
}
4846

47+
static os_log_type_t RCTLogTypeForLogLevel(RCTLogLevel logLevel)
48+
{
49+
if (logLevel < RCTLogLevelInfo) {
50+
return OS_LOG_TYPE_DEBUG;
51+
} else if (logLevel <= RCTLogLevelWarning) {
52+
return OS_LOG_TYPE_INFO;
53+
} else {
54+
return OS_LOG_TYPE_ERROR;
55+
}
56+
}
57+
58+
static os_log_t RCTLogForLogSource(RCTLogSource source)
59+
{
60+
switch (source) {
61+
case RCTLogSourceNative: {
62+
static os_log_t nativeLog;
63+
static dispatch_once_t onceToken;
64+
dispatch_once(&onceToken, ^{
65+
nativeLog = os_log_create("com.facebook.react.log", "native");
66+
});
67+
return nativeLog;
68+
}
69+
case RCTLogSourceJavaScript: {
70+
static os_log_t javaScriptLog;
71+
static dispatch_once_t onceToken;
72+
dispatch_once(&onceToken, ^{
73+
javaScriptLog = os_log_create("com.facebook.react.log", "javascript");
74+
});
75+
return javaScriptLog;
76+
}
77+
}
78+
}
79+
4980
RCTLogFunction RCTDefaultLogFunction = ^(
5081
RCTLogLevel level,
51-
__unused RCTLogSource source,
52-
NSString *fileName,
53-
NSNumber *lineNumber,
82+
RCTLogSource source,
83+
__unused NSString *fileName,
84+
__unused NSNumber *lineNumber,
5485
NSString *message
5586
)
5687
{
57-
NSString *log = RCTFormatLog([NSDate date], level, fileName, lineNumber, message);
58-
fprintf(stderr, "%s\n", log.UTF8String);
59-
fflush(stderr);
88+
os_log_with_type(RCTLogForLogSource(source), RCTLogTypeForLogLevel(level), "%{public}s", message.UTF8String);
6089
};
6190

6291
void RCTSetLogFunction(RCTLogFunction logFunction)

0 commit comments

Comments
 (0)