proof of $-\ln\left(2\sin\left(\frac x2\right)\right)=\sum_{k=1}^\infty \frac {\cos(kx)}{k}$?

There is an identity that I have seen pop up in a few questions on this stackexchange, and I was wondering what proof there is for it. It goes something like this: $$-\ln\left(2\sin\left(\frac x2\right)\right)=\sum_{k=1}^\infty \frac {\cos(kx)}{k}$$

Again, my question is:

How do you prove this identity?


$-\dfrac{\cos(kx)}k=$ real of $-\dfrac{e^{ikx}}k$

As $\ln(1-h)=-\sum_{r=1}^\infty\dfrac{h^r}r,$

$\sum-\dfrac{e^{ikx}}k=\ln\left[1-e^{ix}\right]$

$=\ln(e^{ix/2})+\ln(e^{ix/2}-e^{-ix/2})$

$=ix/2+\ln(i\cdot2\sin x/2)$

$=ix/2+\ln i+\ln(2\sin x/2)$

$i=e^{i\pi/2}\implies\ln(i)=i\pi/2$


Whenever I see a $\cos(kx)$ or $\sin(kx)$ I automatically think of the real or imaginary part of $e^{-kx}$.

So, let $f(x) =\sum_{k=1}^{\infty} \frac{e^{ikx}}{k} $. $f'(x) =\sum_{k=1}^{\infty} ie^{ikx} =\frac{ie^{ix}}{1-e^{ix}} $.

At this stage, I try to integrate $\frac{e^{ax}}{1-e^{ax}}$, so that I can later set $a = i$.

Being lazy, I throw it at Wolfram's integrator and get $\frac{-\ln(e^{ax}-1)}{a} $. I check that yes, this is correct.

Putting $a=i$, I get $\int \frac{ie^{ix}dx}{1-e^{ix}} =\frac{-i\ln(e^{-ix}-1)}{i} =-\ln(e^{-ix}-1) $.

Looking at that result, I get annoyed at myself for not immediately using $-\ln(1-x) =\sum_{k=1}^{\infty} \frac{x^k}{k} $.

At this point, I stop and give this to you to finish, leaving you the task of getting the $\ln$ of that complex expression and separating the real and imaginary parts.