Improved logging function