Rework failed logging. Added tests.