umair13adil / flutter_logs

An extensive logging framework developed for flutter apps.
https://itnext.io/sending-logs-from-flutter-apps-in-real-time-using-elk-stack-mqtt-c24fa0cb9802
Apache License 2.0
39 stars 28 forks source link

Is it possible to export logs file outside of storage, for example to Firebase Storage? #31

Closed Dimius555 closed 2 years ago

Dimius555 commented 2 years ago

I want to get logs file and send it to firebase storage in my app, how can I get it? The question is about iOS version

umair13adil commented 2 years ago

In your pubspec.yaml

dependencies:
   path_provider: ^2.0.6
   firebase_storage: ^10.0.4

You can call the method like this:

Add a future completer:

  static Completer _completer = new Completer<String>();

Logs Exported Callback:

    FlutterLogs.channel.setMethodCallHandler((call) async {
      if (call.method == 'logsExported') {
        // Contains file name of zip
        FlutterLogs.logInfo(
            _tag, "setUpLogs", "logsExported: ${call.arguments.toString()}");

        var zipName = "${call.arguments.toString()}";

        Directory? externalDirectory;

        if (Platform.isIOS) {
          externalDirectory = await getApplicationDocumentsDirectory();
        } else {
          externalDirectory = await getExternalStorageDirectory();
        }

        FlutterLogs.logInfo(
            _tag, "found", 'External Storage:$externalDirectory');

        File file = File("${externalDirectory!.path}/$zipName");

        FlutterLogs.logInfo(_tag, "path", 'Path: \n${file.path.toString()}');

        if (file.existsSync()) {
          FlutterLogs.logInfo(
              _tag, "existsSync", 'Logs found and ready to export!');
          _uploadFile(file);
        } else {
          FlutterLogs.logError(
              _tag, "existsSync", "File not found in storage.");
        }

        // Notify Future with value
        _completer.complete(call.arguments.toString());
      }
    });

Logs Export:

  static Future<String> exportLogs(BuildContext context) async {
    FlutterLogs.exportLogs(exportType: ExportType.ALL);
    return _completer.future as FutureOr<String>;
  }

For Firebase Upload:

   static Future _uploadFile(File file) async {
    try {
      if (file.existsSync()) {
        try {
          Reference storageReference = FirebaseStorage.instance.ref().child(
              "YOUR_FIREBASE_STORAGE_DIRECTORY/");

          Reference storageReference2 =
              storageReference.child("my_logs.zip");

          TaskSnapshot snapshot = await storageReference2.putFile(
            file,
            SettableMetadata(
              contentLanguage: 'en',
              customMetadata: <String, String>{'activity': 'logs'},
            ),
          );
          if (snapshot.state == TaskState.success) {
            final String downloadUrl = await snapshot.ref.getDownloadURL();
            logInfo(_tag, "uploadFile", "Upload Complete! URL: $downloadUrl");
          } else {
            logErrorSimple(_tag, "uploadFile",
                'Error from repo ${snapshot.state.toString()}');
          }
        } catch (e) {
          logError(_tag, "uploadFile", "catchError", e);
        }
      } else {
        logErrorSimple(_tag, "uploadFile", "File not found in storage.");
      }
    } catch (e) {
      logError(_tag, "uploadFile", "catchError", e);
    }
  }
Dimius555 commented 2 years ago

@umair13adil thanks! I will try it now

vysakhvasanth commented 1 year ago

I'm trying to do the similar export, but this callback isn't working for me.