Logging function refactored