Logging in PHP

In javascript, figuring things out is not to bad. For almost everything, I can just use console.log(whatever) and spit out the data I’m trying to figure out right there in the browser.

With PHP is more difficult. You can var_dump() many things. You can improve on that using print_r to make those gnarly1 objects and nested arrays legible.

	//print("<pre>".print_r($a,true)."</pre>");

But for the longest time, I had no good path for logging the information that happens in function that weren’t visible anywhere. I resorted at one point to writing the data using insert_post. Look at the madness below.

//REMOVE FOR PRODUCTION
			$quiz_id = 'quiz id = '.  $data['pro_quizid'];
				$content = ' quiz post id - ' . $id . '<br> disc- ' . $user_discipline . ' curve- ' . $curve . "<pre>".print_r($data['quiz']->ID,true)."</pre>";
				$my_post = array(
			    'post_title'    => 'disc = ' . get_user_discipline($user_id),
			    'post_content'  => $content,
			    'post_status'   => 'publish',
			    'post_author'   => 1,
				);

				Insert the post into the database.
				wp_insert_post( $my_post );

Then, just the other morning, I was feeling particularly unhappy with this pattern and happened upon this post. This little snippet is now my best friend.2

if ( ! function_exists('write_log')) {
   function write_log ( $log )  {
      if ( is_array( $log ) || is_object( $log ) ) {
         error_log( print_r( $log, true ) );
      } else {
         error_log( $log );
      }
   }
}

I can staple that to the bottom of plugins/themes in development and then use write_log() in any number of useful ways. The author mentions using write_log( __LINE__ ); to log if the line in a particular place is every reached and there are many other ways you can tweak this to do what you need. It’s just so nice now to be able to consistently log data from anything in PHP land to a designation location.

It is times like this that I feel bolstered in my opinion that I don’t know anything about programming. It’s also that weird thing where you don’t know what you don’t know. I could have done this search at any time in the past and found the same answer but it seems that I needed to reach a certainly level of unhappiness first. I wonder if I’d have realized what I found earlier or would I have just rolled on. Maybe I have found this before and didn’t realize it for what it was.


1 Not surfer gnarly but unpleasantly tangled gnarly.

2 Granted, it’s not a very competitive list.

Comments on this post

  1. Brian Bennett said on August 30, 2019 at 2:14 pm

    I’ve used this helper in the past to write the result of PHP functions to the console. Super helpful for looking at server-side objects right in the browser. https://stackoverflow.com/a/37218530/2278429

  2. Brian Bennett said on August 31, 2019 at 7:32 am

    I’ve used this snippet in the past which takes the PHP object and logs it to the console. It’s nice because I don’t have to have a separate window open to look at the log. https://stackoverflow.com/a/37218530/2278429