Improve defensiveness of sample logger func

JSON encoding may fail for a number of reasons. Guard against invalid input.