moved log opening in a fucntion