Why doesn't integration by substitution work in this case? I am asked to solve $\int_0^\frac{\pi}{4}\tan x\, \mathrm{d}x$.
This is what I did:
$$
\begin{align}
&\int_0^\frac{\pi}{4}\tan x \,\mathrm{d}x&\\
= {} &\int_0^\frac{\pi}{4}\frac{\sin x}{\cos x}\, \mathrm{d}x&\\
= {} &\int_0^\frac{\pi}{4}\sin x\frac{-1}{-\cos x} \,\mathrm{d}x&\\
= {} &-\int_0^\frac{\pi}{4}\varphi'(x)\frac{1}{\varphi(x)} \,\mathrm{d}x &\text{where $\varphi(x):=-\cos x$} \\
= {} &-\int_{\varphi(0)}^{\varphi(\frac{\pi}{4})} \frac{1}{z} \,\mathrm{d}z \\
= {} &-\int_{-1}^{-\frac{1}{\sqrt{2}}} \frac{1}{z}\, \mathrm{d}z \\
= {} &\Big[\ln z\Big]_{-1}^{-\frac{1}{\sqrt{2}}} \\
= {} &\ln \left(-\frac{1}{\sqrt{2}}\right) - \ln (-1)
\end{align}
$$
And this is where I am stuck, because the solution is definitely not a complex number. I know the correct answer (it's $-\ln\left(\frac{1}{\sqrt{2}}\right)$, my problem is that I don't know where I made a mistake.
 A: You have lost a $-$ sign, so you should actually get
$$\ln\left(-1\right)-\ln\left(-\frac{1}{\sqrt{2}}\right)$$
And now, you can use the fact that 
$$\ln(a)-\ln(b)=\ln\left(\frac{a}{b}\right)$$
to reach the desired result:
$$\ln\left(-1\right)-\ln\left(-\frac{1}{\sqrt{2}}\right)=\ln\left(\frac{-1}{-\frac{1}{\sqrt{2}}}\right)=\ln\left(\frac{1}{\frac{1}{\sqrt{2}}}\right)=\frac{\ln 2}{2}$$
It works, because the logaritm of a negative number (the main branch) $-x$ (where $x>0$) is $\ln(-x)=\ln(x)+i \pi$, and in your case, the $i\pi$'s will cancel each other out. They would not cancel out if you were trying to compute, for example $\int_{-2}^{2} \frac{1}{x} \mathrm{d}x$. In this case, the complex result can tell you that you did something wrong.  
But you can avoid the mess with the complex numbers by simply saying that $\int \frac{1}{x} \mathrm{d}x=\ln|x|$, as José Carlos Santos mentioned it already.
A: You have
$$
\int_a^b\frac{1}{z}\,dz
$$
with $a<0$ and $b<0$. The antiderivative to use is $\ln(-z)$, which is a function defined on $(-\infty,0)$.
You can avoid the problem by writing instead
$$
\int_0^{\pi/4}(-\sin x)\frac{-1}{\cos x}\,dx=
\int_0^{\pi/4}\varphi'(x)\frac{-1}{\varphi(x)}\,dx
$$
where $\varphi(x)=\cos x$. For $x=0$ we have $\varphi(x)=1$; for $x=\pi/4$ we have $\varphi(x)=1/\sqrt{2}$, so the integral becomes
$$
\int_1^{1/\sqrt{2}}-\frac{1}{z}\,dz=
\int_{1/\sqrt{2}}^1 \frac{1}{z}\,dz=
\Bigl[\ln z\Bigr]_{1/\sqrt{2}}^1=-\ln\frac{1}{\sqrt{2}}=\ln\sqrt{2}=
\frac{1}{2}\ln2
$$
A: It turns out that $\displaystyle\int\frac1z\,\mathrm dz=\log|z|$, not $\log z$.
