How to write logging messages to a file in PHP

Being proficient in logging debug or error messages is critical to every programmer. If you used to make programs in any high and robust programming languages such as Java or C#, logging to PHP is boring but it is good enough to capture messages to support your debugging. We are going to discover how to log messages in PHP applications in a minute.

Introduction to the error_log() function

The primary function we use to send messages to a defined stream is error_log. As explained in its documentation, the function will send an error message to the server’s error log or a physical file.

Syntax

The syntax of the function is defined as below:

error_log(
          string $message,
          int $message_type = 0,
          string $destination = ?,
          string $extra_headers = ?): bool;

Parameters

$message
The message should be logged.

$message_type
says where the error should go. It could be any integer value between 0 and 4. Please read the official documentation before implementing your code.

$destination
says whether the message is written out. It depends on the message_type parameter as described above.

A toy project

To demonstrate how the function works, I have pushed a simple project in the blog’s GitHub at the repository. You can clone it to test the function quickly.

The program writes out a timestamped message to a file on the root directory of the application every loading the index.php. It shouldn’t be simple like that in reality. You have to customise your implementation to log error messages instead of dumping anything. An example of the snippet looks like below.

<?php
use \Datetime;

$message = "
<h2>Hello, I am a newbie with PHP programming. Please help me!!!</h2>
<p>See the log file has been newly created un the root directory of this file.</p>
";
echo $message;

$data = array('shopid' => 3,'version' => 1, 'value' => 2);  //here $data is dummy variable
$log_file = $_SERVER['DOCUMENT_ROOT']."/errors-".date("Y-n-j").".log";
date_default_timezone_set('Europe/London');
$date = date('m/d/Y H:i:s', time());
$error_message = "[".$date."] DEBUG: ".json_encode($data)."\n";

error_log(print_r($error_message,true), 3, $log_file);

// In $data we can mention the error message and create the log
How to write log messages to files in PHP
How to write log messages to files in PHP

More readings

  1. Error Handling and Logging, https://www.php.net/manual/en/book.errorfunc.php, accessed on 18/11/2021
  2. Getting started quickly with PHP logging, https://www.scalyr.com/blog/getting-started-quickly-with-php-logging, accessed on 21/11/2021

In summary

We have gone through how to use the error_log function to write out debug and error messages to file systems. If you find the post useful to your work, please help us circulate it. Thank you for your support.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.