It works because one of the most basic rules of logarithms is that
log(px) = p log(x)

This makes sense if you see that the logarithm of x is just the number to which the base (usually 10) must be raised to produce x. For example, the logarithm of 1000 is 3, because the base (10) has to be raised 3 times (10^3) to produce 1000. The logarithm of 10^3 (= 3) is therefore the same as 3 times the logarithm of 10. [ In other words: log(10^3) = 3 log(10) ]

So just take the logarithm of both sides and rearrange.