ios: divert RN logs to our logger

This commit is contained in:
Saúl Ibarra Corretgé 2019-09-04 14:12:36 +02:00 committed by Saúl Ibarra Corretgé
parent f2fdef8361
commit 27205e3119
2 changed files with 60 additions and 1 deletions

View File

@ -53,7 +53,7 @@
registerReactFatalErrorHandler(); registerReactFatalErrorHandler();
// Register a log handler for React. // Register a log handler for React.
registerReactLogHanler(); registerReactLogHandler();
} }
return self; return self;

View File

@ -92,3 +92,62 @@ void registerReactFatalErrorHandler() {
} }
#endif #endif
} }
/**
* A `RTCLogFunction` implementation which uses CocoaLumberjack.
*/
RCTLogFunction _RCTLog
= ^(RCTLogLevel level, __unused RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message)
{
// Convert RN log levels into Lumberjack's log flags.
//
DDLogFlag logFlag;
switch (level) {
case RCTLogLevelTrace:
logFlag = DDLogFlagDebug;
break;
case RCTLogLevelInfo:
logFlag = DDLogFlagInfo;
break;
case RCTLogLevelWarning:
logFlag = DDLogFlagWarning;
break;
case RCTLogLevelError:
logFlag = DDLogFlagError;
break;
case RCTLogLevelFatal:
logFlag = DDLogFlagError;
break;
default:
// Just in case more are added in the future.
logFlag = DDLogFlagInfo;
break;
}
// Build the message object we want to log.
//
DDLogMessage *logMessage
= [[DDLogMessage alloc] initWithMessage:message
level:LOG_LEVEL_DEF
flag:logFlag
context:0
file:fileName
function:nil
line:[lineNumber integerValue]
tag:nil
options:0
timestamp:nil];
// Log the message. Errors are logged synchronously, and other async, as the Lumberjack defaults.
//
[DDLog log:logFlag != DDLogFlagError
message:logMessage];
};
/**
* Helper function which registers a React Native log handler.
*/
void registerReactLogHandler() {
RCTSetLogFunction(_RCTLog);
RCTSetLogThreshold(RCTLogLevelInfo);
}