Skip to content

Commit

Permalink
feat: Re-enable File logger
Browse files Browse the repository at this point in the history
In #1183 we disabled the new File logger mechanism due to a side effect
with react-native-background-geolocation

This was because react-native-file-logger would reset all log appenders
during initialisation which would prevent native-background-geolocation
to work

This commit applies the fix from BeTomorrow/react-native-file-logger#66
(we cannot update the lib as we are still using ReactNative old
architecture)

Related PR: #1183
Related issue: BeTomorrow/react-native-file-logger#64
Related issue: BeTomorrow/react-native-file-logger#66
  • Loading branch information
Ldoppea committed Mar 8, 2024
1 parent e5ddf71 commit d7370b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 80 deletions.
125 changes: 48 additions & 77 deletions patches/react-native-file-logger+0.4.1.patch
Original file line number Diff line number Diff line change
@@ -1,86 +1,57 @@
diff --git a/node_modules/react-native-file-logger/ios/FileLogger.m b/node_modules/react-native-file-logger/ios/FileLogger.m
index 7ee491d..978ce4e 100644
--- a/node_modules/react-native-file-logger/ios/FileLogger.m
+++ b/node_modules/react-native-file-logger/ios/FileLogger.m
@@ -4,6 +4,7 @@
#import <CocoaLumberjack/CocoaLumberjack.h>
#import <MessageUI/MessageUI.h>
#import "FileLoggerFormatter.h"
+#import "RCTLog.h"
diff --git a/node_modules/react-native-file-logger/android/src/main/java/com/betomorrow/rnfilelogger/FileLoggerModule.java b/node_modules/react-native-file-logger/android/src/main/java/com/betomorrow/rnfilelogger/FileLoggerModule.java
index 1f3903d..b687e5b 100644
--- a/node_modules/react-native-file-logger/android/src/main/java/com/betomorrow/rnfilelogger/FileLoggerModule.java
+++ b/node_modules/react-native-file-logger/android/src/main/java/com/betomorrow/rnfilelogger/FileLoggerModule.java
@@ -26,6 +26,7 @@ import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.Appender;
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
@@ -37,6 +38,7 @@ public class FileLoggerModule extends ReactContextBaseJavaModule {
private static final int LOG_LEVEL_INFO = 1;
private static final int LOG_LEVEL_WARNING = 2;
private static final int LOG_LEVEL_ERROR = 3;
+ public static final String APPENDER_NAME = "FileLoggerAppender";

enum LogLevel {
LOG_LEVEL_DEBUG,
@@ -19,6 +20,17 @@ @interface FileLogger () <MFMailComposeViewControllerDelegate>
@end
private static Logger logger = LoggerFactory.getLogger(FileLoggerModule.class);

@implementation FileLogger
+{
+ NSMutableDictionary *_promisesList;
+}
+
+- (instancetype)init
+{
+ if ((self = [super init])) {
+ _promisesList = [[NSMutableDictionary alloc] init];
+ }
+ return self;
+}
@@ -69,6 +71,7 @@ public class FileLoggerModule extends ReactContextBaseJavaModule {

RCT_EXPORT_MODULE()
RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<>();
rollingFileAppender.setContext(loggerContext);
+ rollingFileAppender.setName(APPENDER_NAME);
rollingFileAppender.setFile(logsDirectory + "/" + logPrefix + "-latest.log");

@@ -101,6 +113,8 @@ - (dispatch_queue_t)methodQueue {
[composeViewController setMessageBody:body isHTML:NO];
}

+ _promisesList[RCTKeyForInstance(composeViewController)] = @[resolve, reject];
+
NSArray<NSString*>* logFiles = self.fileLogger.logFileManager.sortedLogFilePaths;
for (NSString* logFile in logFiles) {
NSData* data = [NSData dataWithContentsOfFile:logFile];
@@ -112,13 +126,44 @@ - (dispatch_queue_t)methodQueue {
presentingViewController = presentingViewController.presentedViewController;
if (dailyRolling) {
@@ -108,15 +111,24 @@ public class FileLoggerModule extends ReactContextBaseJavaModule {
rollingFileAppender.setEncoder(encoder);
rollingFileAppender.start();

- ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
- root.setLevel(Level.DEBUG);
- root.detachAndStopAllAppenders();
- root.addAppender(rollingFileAppender);
+ this.renewAppender(rollingFileAppender);

configureOptions = options;
promise.resolve(null);
}
[presentingViewController presentViewController:composeViewController animated:YES completion:nil];
-
- resolve(nil);
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
+ NSString *key = RCTKeyForInstance(controller);
+
+ NSMutableArray *promise = _promisesList[key];
+ if (promise) {
+ RCTPromiseResolveBlock resolve = promise[0];
+ RCTPromiseRejectBlock reject = promise[1];
+ switch (result) {
+ case MFMailComposeResultSent:
+ resolve(@"sent");
+ break;
+ case MFMailComposeResultSaved:
+ resolve(@"saved");
+ break;
+ case MFMailComposeResultCancelled:
+ resolve(@"cancelled");
+ break;
+ case MFMailComposeResultFailed:
+ reject(@"failed", error.localizedDescription, nil);
+ break;
+ default:
+ reject(@"error", @"Unknown error", nil);
+ break;
+ private void renewAppender(Appender appender) {
+ ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
+ root.setLevel(Level.DEBUG);
+ // Stopping the previous appender to release any resources it might be holding (file handles) and to ensure a clean shutdown.
+ Appender previousFileLoggerAppender = root.getAppender(APPENDER_NAME);
+ if (previousFileLoggerAppender != null) {
+ previousFileLoggerAppender.stop();
+ root.detachAppender(APPENDER_NAME);
+ }
+ [_promisesList removeObjectForKey:key];
+ } else {
+ RCTLogWarn(@"No promise registered for mail: %@", controller.title);
+ root.addAppender(appender);
+ }
+
[controller dismissViewControllerAnimated:YES completion:nil];
}

+static NSString *RCTKeyForInstance(id instance)
+{
+ return [NSString stringWithFormat:@"%p", instance];
+}
+
@end

@ReactMethod
public void write(int level, String str) {
switch (level) {
3 changes: 0 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@ import LauncherView from '/screens/konnectors/LauncherView'
import { useShareFiles } from '/app/domain/osReceive/services/shareFilesService'
import { ClouderyOffer } from '/app/view/IAP/ClouderyOffer'
import { useDimensions } from '/libs/dimensions'
// Temporarily disable FileLogger until we fix issue https://github.com/BeTomorrow/react-native-file-logger/issues/64
/*
import { configureFileLogger } from '/app/domain/logger/fileLogger'

configureFileLogger()
//*/

// Polyfill needed for cozy-client connection
if (!global.btoa) {
Expand Down

0 comments on commit d7370b7

Please sign in to comment.