(Original answer below the line was more mechanical in how the operations work out. Edit at the top addresses some theoretical issues.)
Empirically we have identified certain "dimensions" like length, time, and mass that correspond to phenomena in the world. Historically, we assigned "units" so that we could compare information about those dimensions. Later we understood that there are fundamental constants of nature that are themselves measurable. The constants provide relationships between units that, historically, we said belonged to different dimensional quantities. For example $c = 3 \times 10^8\ \mathrm{m/s}$ (up to some rounding that's beside the point for now). That's a relationship between one constant and two units.
You could take a different perspective on that equation, however, and think of it as one equation for three unknowns. That let's us eliminate one unknown, but we're stuck with two that physics doesn't (apparently) uniquely determine. From this perspective those are real numbers in an equation, they are just not numbers that we can uniquely determine. So we have to carry them around as appropriate multiplicative factors in expressions.
Now look at something like $\log(5\ \mathrm{m})$. For a lot of good reasons we usually do NOT want to write a log with a unit like that. But it's not ill-defined mathematically as 5 is clearly a real number and, as above, we can think of $\mathrm{m}$ as a real (but unknown) number as well. We also know that it's not zero from the equation and by convention we choose both it and $\mathrm{s}$ to be positive. So, from that perspective, we don't know what specific numerical value to assign to $\log(5\ \mathrm{m})$ but we should expect that it has some value and that, more importantly for your original question, that it obeys the usual rules of log.
Once we accept that it obeys the usual rules of log, then your problem about inconsistent dimensions on the two sides of your equations goes away via a cancelation. Now follows the original answer that get's specific in how that plays out for a sample calculation of differences of log.
(Original answer)
You need to get consistency of units. With the logarithm that can happen in two ways. One way is that the argument is unitless and then the log of that is also unitless. That can happen with a ratio of two values that have units or because you started with values that had no units somehow.
For simplicity of explanation, we usually say that the arguments to the log must be dimensionless, but that's not quite precise. (Though if you ensure it, then you are always good, which why it's stated as the "rule".)
If you want to be general, the unit, is essentially a constant multiplicative factor, so several of the answers are wrongly stating things about expression of the form $\log (x/y) = \log x - \log y$. This is well-formed even when the variables represent dimensionful quantities.
For example, assume they are lengths, and pick some numbers for a sample calculation. Then, e.g.,
$$ \log(5\ \mathrm{m} / 2\ \mathrm{m}) = \left[ \log(5) - \log (2)) \right] + \left[ \log(\mathrm{m}) - \log(\mathrm{m}) \right] = \log(5) - \log (2)) $$
So you get the same answer whether you cancel the units in the expression all the way on the left "first" or if you carry them through the middle expression, so long as you are consistent.
Technically this even works if you use different units for length, but then you have some nasty expressions involving logs of units that don't cancel:
$$ \log(5\ \mathrm{m} / 2\ \mathrm{mm}) = \left[ \log(5) - \log (2)) \right] + \left[ \log(\mathrm{m}) - \log(\mathrm{mm}) \right] \neq \log(5) - \log (2)) $$
So in practice we usually ensure that this doesn't happen. It's not wrong per se, but it's practically useless. Note that you can still work through the mixed-form in a consistent manner though.
You could do it the conventional way:
$$ \log(5\ \mathrm{m} / 2\ \mathrm{mm}) = \log(5000\ \mathrm{mm} / 2 \ \mathrm{mm}) = \log(5000/2) = \log 5000 - \log 2 $$
Or equally well:
$$ \begin{align}
\left[ \log(5) - \log (2)) \right] + \left[ \log(\mathrm{m}) - \log(\mathrm{mm}) \right]
& = \left[ \log(5) - \log (2)) \right] + \left[ \log(1000\ \mathrm{mm}) - \log(\mathrm{mm}) \right] \\
& = \log 5 - \log 2 + \log 1000 = \log 5000 - \log 2
\end{align} $$