Thursday, July 27, 2017

Awesome Way on Creating a Log Files with PHP


Ohsomedevs Logging using file PHP banner.

There are times, we feel that we are in need a file which logs a request or transaction from a process we are currently working. Although we can fetch it automatically on our database but what if we don't need to store the data on the database and we only need to put it as a log on a simple text file.

More often, we usually encounter some errors or successes during our development. It's a good practice to save these responses for us to know why  we having errors.



So, in this tutorial, we'll going to log the different responses from a server request using PHP.



Things to note:

  • There must be a valid directory where the text log file will be stored.
  • Good practice for naming file is putting the date on its name. (Ex. success_log_07-07-2017.txt)

Using PHP's file_put_contents function


The  fopen(), fwrite() and fclose() are very identical function with file_put_contents() on writing data in the file. In file_put_contents(), if the file does not exist, it will automatically create the file. On other hand, it will rewrite the file.


$data_array = array(
 'name'   => 'John Snow',
 'email'  => 'john_snow@got.com',
 'username' => 'john123',
 'password' => '123'
);

$log_data = [];

$log_data['message'] = "------------------------------------ ". date("F j, Y, g:i a") ." ---------------------------------------".PHP_EOL;
if ( strlen($data_array['password']) < 5  ) {
 $log_data['message'] .= "Error: Password length is less than 5 characters." . PHP_EOL;
 $log_data['status']  = 'error';
} else {
 $log_data['message'] .= "Success: Valid password lenght." . PHP_EOL;
 $log_data['status']  = 'success';
}
echo "Check your application folder and you will see the generated file.";
file_put_contents($log_data['status'].'_log_'.date("j-n-Y").'.txt', $log_data['message'], FILE_APPEND);


On the sample code above, we wanted to check out on what's inside the error message log file. As we can see on the if statement condition, we set that if the password is less than 5 characters in length, it will throw and error message and will write up to a file which will be our error log file.


Below, we will be going to make the password valid based on our conditions, we will make the password's string length into 6 characters and it should be validated based on our condition. 

$data_array = array(
 'name'   => 'John Snow',
 'email'  => 'john_snow@got.com',
 'username' => 'john123',
 'password' => '123456'
);

$log_data = [];

$log_data['message'] = "------------------------------------ ". date("F j, Y, g:i a") ." ---------------------------------------".PHP_EOL;
if ( strlen($data_array['password']) < 5  ) {
 $log_data['message'] .= "Error: Password length is less than 5 characters." . PHP_EOL;
 $log_data['status']  = 'error';
} else {
 $log_data['message'] .= "Success: Valid password length." . PHP_EOL;
 $log_data['status']  = 'success';
}
echo "Check your application folder and you will see the generated file.";
file_put_contents($log_data['status'].'_log_'.date("j-n-Y").'.txt', $log_data['message'], FILE_APPEND);

So in this case, we generated a success log file since the password has a valid string length. 



This will be the contents of the log files we generated:

error_log_27-7-2017.txt
------------------------------------ July 27, 2017, 5:50 pm ---------------------------------------
Error: Password length is less than 5 characters.


success_log_27-7-2017.txt
------------------------------------ July 27, 2017, 5:57 pm ---------------------------------------
Success: Valid password length.



Voila! Creating log files using PHP is very essential specially on the server stuffs. You can play around with the file_put_contents() function and make its own rule based on you needs. Happy coding fellas!








No comments:

Post a Comment

PHP IMAP - Get Emails from GMAIL

Fetching emails from your GMIAL account is easier than what you expected. With the use of PHP IMAP Extension, you can easily fetch your e...