Evaluate $$\int_0^{\infty} \frac{(\log x)^2}{1+x^2} dx$$ using contour integration.
First step:
$$\oint \frac{(\log z)^2}{1+z^2} dz$$
the path taken and branch cut are as shown:

The residue at $z=i$ is equal to $i\frac{π^2}{8}$, and so
$$\oint \frac{(\log z)^2}{1+z^2} dz = \int_{-R}^{-ε} \frac{(\log x)^2}{1+x^2} dx + \int^0_π\frac{\log εe^{iθ}}{1+ε^2e^{2iθ}}iεe^{iθ}dθ +\int_{ε}^{R} \frac{(\log x)^2}{1+x^2} dx + \int^π_0\frac{\log Re^{iθ}}{1+R^2e^{2iθ}}iRe^{iθ}dθ =-π^3/4 $$ by Cauchy's integral formula.
In the limit $R\rightarrow \infty$ and $ε\rightarrow 0$ the integrals around the large and small semi-circles are supressed as $(\log R)^2/R$ and $ε(\log ε)^2$ respectively, so we are left with:
$$\int_{-R}^{-ε} \frac{(\log x)^2}{1+x^2} dx +\int_{ε}^{R} \frac{(\log x)^2}{1+x^2} dx =-π^3/4 $$
To continue from here, at first I try
$$\int_{-\infty}^{0} \frac{(\log x)^2}{1+x^2} dx +\int_{0}^{\infty} \frac{(\log x)^2}{1+x^2} dx =2\int_{0}^{\infty} \frac{(\log x)^2}{1+x^2} dx =-π^3/4 $$
but it turns out this answer is off by a minus sign.
The correct method (according to my textbook) is instead to do
$$\oint \frac{(\log z)^2}{1+z^2} dz = \int_{-R}^{-ε} \frac{(\log (|x|e^{iπ}))^2}{1+x^2} dx +\int_{ε}^{R} \frac{(\log x)^2}{1+x^2} dx = -π^3/4 $$ and then expand out the$ (\log (|x|e^{iπ}))^2 = (\log |x|)^2+2iπ\log |x|-π^2$ and compare real and imaginary parts to the RHS.
I don't understand why we should have go from $$\int_{-R}^{-ε} \frac{(\log x)^2}{1+x^2} dx $$ to $$\int_{-R}^{-ε} \frac{(\log (|x|e^{iπ}))^2}{1+x^2} dx \left( = \int_{-R}^{-ε} \frac{(\log (-x))^2}{1+x^2} dx (?)\right)$$ to get the integral to work.
I understand vaguely there is some notion of keeping track of the angle θ in contour integration to avoid issues with branch cuts etc., but in this particular case I don't see how its relevant. Also the change that has been made seems to introduce a minus sign in the log where the previously wasn't one (the RHS in curved brackets). Could someone explain why my textbook's method works?
First, the complex logarithm $\log(z)$ is defined as $\log(z)=\log(|z|)+i\arg(z)$, where $\arg(z)$ is multi-valued. With the branch cut along the non-negative real axis, $\arg(z)\in [0,2\pi)$.
On the negative real axis, $\arg(z)=\pi$ and $\log(z)=\log(|z|)+i\pi=\log(|x|)+i\pi$. Then, we can write
$$\begin{align} \int_{-R}^{-\varepsilon} \frac{\log^2(z)}{z^2+1}\,dz+\int_\varepsilon^R \frac{\log^2(z)}{z^2+1}\,dz&=\int_{-R}^{-\varepsilon} \frac{(\log(|x|)+i\pi)^2}{x^2+1}\,dx+\int_\varepsilon^R \frac{\log^2(x)}{x^2+1}\,dx\\\\ &=\int_\varepsilon^R \frac{2\log^2(x)+i2\pi \log(x)-\pi^2}{x^2+1}\,dx \end{align}$$
Can you finish now?