Factor logging guts into generic function.