added start of logging function, redefining variable scope