Refactor debug logging functionality