sendLogsToServer method

dynamic sendLogsToServer()

Implementation

sendLogsToServer() async {
  if (logList.isEmpty) return;

  if (!_mustSendLogToServer) {
    _mustSendLogToServer = kReleaseMode ? true : false;
  }

  if (_mustSendLogToServer) {
    if (_secret.isEmpty || _platformName.isEmpty || _baseUrl.isEmpty) {
      if (!_mustSendLogToServer) {
        // throw Exception("Logger is not initialized. Call init() first.");
      }
    }
    try {
      var url = _baseUrl;
      String platform = await getPlatformInfo();
      Map<String, dynamic> requestBody = {
        "logs": logList.map((log) {
          return {
            'level': log.level,
            'title': log.title,
            'enviroment': log.enviroment,
            "message": log.message,
            "jsonData": log.jsonData ?? "",
            "platform": platform,
            if(_user != null) "user": _user,
          };
        }).toList(),
      };
      final response = await dio.post(
        url,
        options: Options(
          headers: {
            "Authorization": hashWithSha256(secret: _secret),
            "Platform-name": _platformName,
          },
        ),
        data: requestBody,
      );
      if (response.statusCode == 200) {
        logList.clear();
      } else {
        if (!_mustSendLogToServer) {
          // throw Exception(
          //     'Failed to log message with status code ${response.statusCode}');
        }
      }
    } catch (exception) {
      if (_mustSendLogToServer) {
        // Handle other exceptions here
        // print(exception);
      }
    }
  }
}