Start of log writing function
Lets fix this mess!