in reply to Capturing Error message in a log file.

Looks like the problem is getting $debug_output to contain the error message.

Since it looks like you're passing the desired message string as an argument to debug, then the thing to do is set $debug_output from the argument. Like so:
sub debug { my $debug_output = shift; if ($debug_output) {
Are you using strict? If not, then you probably should, because it would have helped you locate the source of your problem.

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.