Note that your alternative antiderivative can be rewritten as
$$
f(x) = \begin{cases}
\ln|x| + A, &x < 0 \\
\ln|x|+B, & x > 0
\end{cases}
$$
for constants of integration $A,B$. If you find the derivative, you'll see no problems and that $f'(x) = \frac 1 x$ for $x \ne 0$ all the same.
The phenomenon here is that the domain of $\frac 1 x$ consists of two sets, $(-\infty,0)$ and $(0,\infty)$, which are
- open
- connected
- maximal (w.r.t. set inclusion within the "universe" $\mathbb{R} \setminus \{0\}$), i.e. there is no bigger open connected set you can use in $\mathbb{R}$ that contains $(-\infty,0)$ without also containing $0$ which is not permitted in the domain of $\frac 1 x$
On each of these sets, you are allowed a different constant of integration and no contradictions will arise. Hence $f$ as written above may be considered the "true" antiderivative of $f$, as opposed to merely $\ln|x|+C$ as some students may be inclined to think.
You can play around with this in a Desmos demo I made a while back. Notice that adjusting each constant of integration only vertically translates a particular half of the graph on the side of the asymptote, thus not changing its rate of change (derivative).
Another example would be for
$$
f(x) = \frac{1}{1-x^2}
$$
which will have antiderivative
$$
F(x) = \begin{cases}
\frac 1 2 \ln \left| \frac{1-x}{1+x} \right|+A, & x < -1 \\
\frac 1 2 \ln \left| \frac{1-x}{1+x} \right|+B, & -1 < x < 1 \\
\frac 1 2 \ln \left| \frac{1-x}{1+x} \right|+C, & x > 1
\end{cases}
$$
Here is a similar Desmos demo for it.