Add logging

This is just copying upstream, so it's probably worth breaking out into a separate change (perhaps before I suggest my enhancements).