Simple PHP error logging script – organize your logs

Simple PHP error logging script – made easy!

When you are making an application, at some point you will need to log your errors, or warnings in a file in order to keep a track of what is going on. In the most cases the web server is logging the exceptions, but what if we need to separate our application’s logs in a different folder, or different file.

Here is a simple solution that will allow us to do it – in a small and simple PHP error logging script.

Let’s create our first file and name it Logger.php.

We will place some methods inside of this Logger class, in order to make our job easy. What we need is the following:

  • A main method
  • A method that generates a file name with the current date
  • A method that returns the current date
  • A method that writes the content to a file

I’m trying to keep everything simple and organised for you, hence the many methods.

Ok, let’s create our simple PHP error logging script class.

Logger.php

[code language=”php”]
<?php

class Logger {

/* Declare the time zone */
protected $timeZone = ‘Europe/Sofia’;

/* Our main simple PHP error logging script method */
public function log($type, $message) {
$currentDate = $this->getCurrentDate(); // Get the current date using the getCurrentDate() function
$fileName = $this->generateFileName(); // Getnerate file name using generateFileName() function
$file = ‘/logs/’ . $fileName; // Declare our file

/* Write the content to the log file */
$this->writeFile($file, $type, $currentDate, $message);
}

/* Getting the current date for our time zone */
protected function getCurrentDate() {
date_default_timezone_set($this->timeZone); // Set the time zone
$current_date = date(‘d-m-Y|H:i:s’); // Formatting the date

return $current_date; // Returns the newly created date
}

/* Generating a file name based on the currentdate */
protected function generateFileName() {
date_default_timezone_set($this->timeZone);
$newFileName = date(‘d-m-Y’) . ".log"; // Create the file name

return $newFileName;// Return the file name
}

/* Writing the content to the file – the core of our simple PHP error logging script */
protected function writeFile($file, $type, $currentDate, $message) {
$logMessage = "";

/* Check the log type and write it to the file */
if ($type == "info") {
$logMessage = "[INFO] $currentDate: $messagen";
}
if ($type == "error") {
$logMessage = "[ERROR] $currentDate: $messagen";
}

file_put_contents($file, $logMessage, FILE_APPEND | LOCK_EX);
}
}
[/code]

Now we have our very own simple PHP error logging script, created in just few minutes. But how are we going to use the above script?
It’s easy! You can call the main method whenever you like by the following code:

[code language=”php”]
include ‘Logger.php’; // Don’t forget to include our logger!

$logger = new Logger(); // Create an instance of the class
$logger->log("info", "Your message here"); // For an info message
$logger->log("error", "Your error message, or exception here"); // For error message
[/code]

Congratulations! Now you have your working simple PHP error logging script!

– Use it wise and keep a better record of your application events.

Leave a Reply