Change logger into a goroutine.

- Change logger to use goroutine/channel/waitgroup instead of a lock.
Reorganize code