In addition to set -x I have taken to wrapping my main entry point in some scripts where record keeping is helpful in a sub shell and pipe all output to a function that tees it's output to a log file after cleaning up escape sequences so I can generate a log file without having to annotate every line with some kind of wrapper:
#!/usr/bin/env bash
(
foo
bar
) 2>&1 | print_and_log "$logfile"