Real integral by keyhole contour Evaluate
$$\int_0^\infty \frac{\log x \; dx}{x^{2} + 2x + 2}$$
by integrating a branch of $(\log z)^{2}/(z^{2} + 2z +2)$ along a keyhole contour.
The thing I have trouble with is why I should be examining the square of the log - I guess it has something to do with ln x in fact NOT being the real part of log z, since log z is (or can be) defined on the entire negative real axis and ln x can't. But looking at the square would definitely not have been my first plan of attack :(
 A: This Wikipedia page goes through a very similar example in detail. The reason for the square there is that the $\log(z)^2$'s from two halves of the contour cancel, but they leave behind a cross term that doesn't cancel, which allows you to compute the original integral. I find it rather magical. Put another way, try it without the square and the $\log(z)$'s will cancel, so that's no good.
A: Here are some details of the calculation. Let the keyhole contour be oriented counterclockwise and have the slot on the positive real axis. Call the segment above the real axis $\Gamma_1$, the large circle of radius $R$  $\Gamma_2$, the segement below the real axis $\Gamma_3$ and the circle of radius $\epsilon$ around the origin $\Gamma_4.$  We use the branch of the logarithm with the cut along the positive real axis and returning an argument from zero to $2\pi.$ Note that the poles of $$\frac{1}{z^2+2z+2}$$ are at
$$(z+1)^2 + 1 = 0$$ or
$$\rho_{0,1} = -1 \pm i.$$
Calling the desired integral $I$, we have 
$$\left(\int_{\Gamma_1} + \int_{\Gamma_2} + \int_{\Gamma_3} + \int_{\Gamma_4}\right)
\frac{\log^2 z}{z^2+2z+2} dz
\\= 2\pi i 
\left(\mathrm{Res}\left(\frac{\log^2 z}{z^2+2z+2}; z = -1 + i\right)
+ \mathrm{Res}\left(\frac{\log^2 z}{z^2+2z+2}; z = -1 - i\right)\right).$$
Now along the large circle we have
$$\left|\int_{\Gamma_2} \frac{\log^2 z}{z^2+2z+2} dz\right|
\sim 2\pi R \times \frac{\log^2 R}{R^2} \rightarrow 0$$
as $R\rightarrow\infty.$
Along the small circle we get
$$\left|\int_{\Gamma_4} \frac{\log^2 z}{z^2+2z+2} dz\right|
\sim 2\pi \epsilon \frac{\log^2\epsilon}{2} \rightarrow 0$$ 
as $\epsilon\rightarrow 0$ by repeated application of L'Hôpital's rule.
Now the residues are easy to compute because the poles are simple and we obtain
$$\mathrm{Res}\left(\frac{\log^2 z}{z^2+2z+2}; z = -1 + i\right)
= \frac{1}{2i} \log^2(-1+i)$$
and $$\mathrm{Res}\left(\frac{\log^2 z}{z^2+2z+2}; z = -1 - i\right)
= - \frac{1}{2i} \log^2(-1-i)$$
As we actually do the computation of these residues we need to be careful to use the same branch of the logarithm as in the integral. A computer algebra system might use a different branch!
For the first residue we get
$$\frac{1}{2i} \left(\frac{1}{2}\log 2 + \frac{3}{4} i\pi\right)^2
= \frac{1}{2i} 
\left(\frac{1}{4}\log^2 2 - \frac{9}{16} \pi^2 + \frac{3}{4}\log 2 \times i\pi\right)$$ 
and for the second one
$$-\frac{1}{2i} \left(\frac{1}{2}\log{2} + \frac{5}{4} i\pi\right)^2
= -\frac{1}{2i} 
\left(\frac{1}{4}\log^2 2 - \frac{25}{16} \pi^2 + \frac{5}{4}\log 2 \times i\pi\right)$$ 
Adding these contributions yields
$$\frac{1}{2i} \left(\pi^2 - \frac{1}{2}i\pi \log 2\right).$$
Finally we get
$$\left(\int_{\Gamma_1} + \int_{\Gamma_3} \right)
 \frac{\log^2 z}{z^2+2z+2} dz = 2\pi i \times
\frac{1}{2i} \left(\pi^2 - \frac{1}{2}i\pi \log 2\right)
= \pi^3 - \frac{1}{2}i\pi^2 \log 2$$
Observe that along $\Gamma_3$ the logarithm term produces (actually $x$ would be a better choice of variable here rather than $z$)
$$-(\log^2 z + 4\pi i \log z -4\pi^2).$$
The first of these cancels the integral along $\Gamma_1$ and the third is real so that equating imaginary parts we find
$$I = \int_{\Gamma_1}  \frac{\log z}{z^2+2z+2} dz
= - \frac{1}{4\pi} \left(- \frac{1}{2} \pi^2 \log 2\right) = \frac{1}{8} \pi\log 2.$$
We also get the following bonus integral (comparing real parts)
$$ \int_{\Gamma_1}  \frac{1}{z^2+2z+2} dz = \frac{\pi^3}{4\pi^2} = \frac{\pi}{4}.$$
