Home arrow PHP arrow Page 2 - Logging With PHP

The Bare Necessities - PHP

Want to log script activity in your Web application, but haveno clue where to begin? Take our handy tour of PHP's logging functions,and find out how simple it really is.

TABLE OF CONTENTS:
  1. Logging With PHP
  2. The Bare Necessities
  3. Turning The Tables
  4. Turning Up The Heat
  5. Biting Into A PEAR
  6. Destination Unknown
  7. Artificial Intelligence
  8. Big Brother Is Watching
  9. Closing Time
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 53
November 27, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Logging data to a file in PHP can be as simple or as complex as you want to make it. Break it down, though, and it all comes down to these three simple lines of code:

<?php // open file $fd = fopen($filename, "a"); // write string fwrite($fd, $str . "\n"); // close file fclose($fd); ?>
Fundamentally, logging data to a file consists of three steps:

1. Open the target file (or create it if it doesn't already exist);

2. Append your data to the end of the file;

3. Close the file.

You can encapsulate this as a function,

<?php function logToFile($filename, $msg) { // open file $fd = fopen($filename, "a"); // write string fwrite($fd, $msg . "\n"); // close file fclose($fd); } ?>
and then use it liberally within your code as and when required.

<?php function logToFile($filename, $msg) { // open file $fd = fopen($filename, "a"); // write string fwrite($fd, $msg . "\n"); // close file fclose($fd); } $v = "Mary had a little lamb"; if (!is_numeric($v)) { logToFile("my.log", "Non-numeric variable encountered"); } $a = array("chocolate", "strawberry", "peach"); if (!in_array('fish', $a)) { logToFile("my.log", "No fish available"); } $conn = @mysql_connect("localhost", "joe", "pass"); if (!$conn) { logToFile("my.log", "Could not connect to database"); die("Could not connect to database"); } ?>
You can make the function a little more professional by having it automatically append the date and time of the log message to the log file as well:

<?php function logToFile($filename, $msg) { // open file $fd = fopen($filename, "a"); // append date/time to message $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg; // write string fwrite($fd, $str . "\n"); // close file fclose($fd); } ?>
Here's an example of the output:

[2002/11/25 06:02:42] Non-numeric variable encountered [2002/11/25 06:02:42] No fish available [2002/11/25 06:02:43] Could not connect to database


 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: