Refactoring logging logic