Use log cache atomically.

The current method has a data race when multiple log calls happen concurrently. With this patch the cache update is atomic but still doesn't require a mutex.