Need help with a logging function that I tweaked

Today I added a line to this logging function so that it would keep a running tab of everything being written to the log, so that I could also email that content to me. This is represented by $log4RobertEmail.

function log_message($message, $type) {

$date = date('m/d/Y h:i:s a', time());
$message = "$date - $type - $message \n";
error_log($message, 3, '/var/log/bounceCheck');
$log4RobertEmail .= "$message \n";

}

And here is what I use elsewhere in the same PHP code in various places:

$log_post = “Reached line 083 (Start processing next email)”;
log_message($log_post, ‘START TIME’);

Everything is written to the actual log just fine, but when I email the contents of $log4RobertEmail, nothing is printed in the email. What am I doing wrong?

Thanks!

I can’t see anything wrong in the code you posted. Can you please also post the code that send the e-mail.

Also, have you considered using a logging package like Monolog? They’ve solved all these problems so you won’t have to :slight_smile:

Php functions have local variable scope. $log4RobertEmail doesn’t exist outside that function and is destroyed when each call to that function ends. You would be getting a php undefined variable error when trying to reference the variable in the email code.

You can make this ‘work’ by making $log4RobertEmail a main program variable, then using the global keyword inside the function to break variable scope.

1 Like

So just put $log4RobertEmail = null at the top of the PHP script before the function?

Please do not communicate things through global variables :eek:

What is the idea behind the email, will it send the entire log for each request to you via email?
Also, can you show us the code that’s supposed to send the email?

1 Like

The idea behind the email is to send me a log of events that the bounceChecker script runs through while processing incoming bounced emails, unsubscribe request emails, etc. So yes, I can monitor what is happening in the actual log file, but it would also be nice to have those details emailed to me for problematic emails the bounce processor was not able to successfully take care of.

Here is the code to send the email, very simple:

  if ($notifyRobert) {
       
  	$log_post = "Reached line 670 (Robert will be notified by email)";
  	log_message($log_post, 'LINE LOG');
  				
  	if ($emailSubject) {
  		
  		// Use the subject created ealier in this code.
  	
  	} else {
  		
  		// Otherwise use this subject:
  		$emailSubject = "BounceCheck: Unable to determine action";
  	}
  	
  	$justEmailID = preg_replace('~\D~', '', $subject);
  	
      $emailMessage .= "Subject:\n\n";
      $emailMessage .= "$subject\n\n";	        
      $emailMessage .= "EmailID if found in the subject line:\n\n";
      $emailMessage .= "$justEmailID\n\n";	
      $emailMessage .= "BounceCheck log:\n\n";
      $emailMessage .= "$log4RobertEmail\n\n";
      $emailMessage .= "Body:\n\n";
      $emailMessage .= "$body\n\n";
      $emailMessage .= "Full email content processed by BoogieTools:\n\n";
      $emailMessage .= "$messageContent\n";		            
   	$recipients = array('robert@domain.com');
   	$toAddresses = implode(',', $recipients);
   	$headers =	'From: support@domain.com' . "\r\n" .
  				'Reply-To: support@domain.com' . "\r\n" .
  				'X-Mailer: PHP/' . phpversion();
   	
   	mail($toAddresses,$emailSubject,$emailMessage,$headers);
  }

I personally hate global variables. To me, the whole point of a function is that it is a piece of standalone code than can be called from anywhere, without relying on anything other than what is passed in. In the OPs case I’d want to return the new log string from the function:

function log_message($message) { 
  // build up the message string as you already do, and write to the log
  $msg = $date . " " . $time . " " . $message;
  return $msg; // pass it back to the calling code
  }

$LogToDave .= log_message("Add a log entry here");

// more code 

// more code

$LogToDave .= log_message("Add another log entry here");