Adding log files in PHP

Create a function that would write logs. Pretty straightforward, isn’t it:

function writelog($log_entry, $log_file){

  $filehandle = fopen($log_file, "a"); //open the file for append
  fwrite($filehandle, $log_entry);
  fclose($filehandle);
}

Now, printing strings to the function is as straightforward. But printing Arrays to the function was quite a pain until I figured out the answer:

The following statement would print arrays or objects to the file:

writelog("array_to_print:  ".print_r($result_array, true)."\n\n", $log_file_name);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s