Simply take the $\log$! So,
$$\text{level } = \left \lfloor \log_2{(\text{index})} \right \rfloor$$
- The root (index$=1$) is at level $\log_2 1 = 0$
- The elements at index $2, 3$ are both at level $\left \lfloor \log_2{2} \right \rfloor = \left \lfloor \log_2{3} \right \rfloor = 1$
- The elements at index $4,5,6,7$ are all at level $\left \lfloor \log_2{4} \right \rfloor = \left \lfloor \log_2{5} \right \rfloor = \left \lfloor \log_2{6} \right \rfloor = \left \lfloor \log_2{7} \right \rfloor = 2$
- And so on.
If you wish to continue with your approach:
Let both $a$ and $b$ be at the level $k$.
Number of nodes before $a$ can be given as: $2^0 + 2^1 + 2^2 + \dots + 2^{k-1}$. This huge sum is just the sum of a Geometric Progression, and is equal to $2^k - 1$.
So, $a$, the first element at level $k$ is infact the $2^k$th element of the tree! (This is a nice property of complete trees)
Since $b$ is the last element of level $k$, the index of $b$ is simply $2^{k+1}-1$.
Now, let us assume that the element $X[i]$ is on the level $L$.
Since the indices of the first and last elements on the level $L$ are $2^L$ and $\left (2^{L+1}-1 \right )$ respectively, we know that $i$ must be within that range. So, $$2^L \quad \leq \quad i \quad \leq \quad 2^{L+1}-1$$
Taking $\log_2$, we get: $$L \quad \leq \quad \log_2{i} \quad \leq \quad L+1 - \varepsilon$$
$\varepsilon$ is a small error which was introduced when we ignored the $-1$ while taking $\log$ of $2^{L+1}-1$
So, we have: $$L = \left \lfloor \log_2 i \right \rfloor$$