Some logging cleanups

The commits tell the story here - this is just a bunch of bug fixes and cleanups to the logging middleware stuff.

@jcredding ready for review