Rewrite logging to be more flexible