Posted By

evinweissenberg on 08/18/12


Tagged

php csv logging


Versions (?)

Event Logger


 / Published in: PHP
 

URL: http://www.evinw.com

Append Log Event to a .csv file.

  1. <?php
  2. /*
  3. |---------------------------
  4. | Author: Evin Weissenberg
  5. |---------------------------
  6. */
  7. class Log_Event {
  8.  
  9. private $event_message;
  10. private $file_path;
  11. private $failed_message;
  12. private $time_zone;
  13.  
  14. function setEventMessage($event_message) {
  15.  
  16. $this->event_message = (string)$event_message;
  17. return $this;
  18.  
  19. }
  20.  
  21. function setFilePath($file_path) {
  22.  
  23. $this->file_path = (string)$file_path;
  24. return $this;
  25.  
  26. }
  27.  
  28. function setFailedMessage($failed_message) {
  29.  
  30. $this->failed_message = (string)$failed_message;
  31. return $this;
  32.  
  33. }
  34.  
  35. function setTimeZone($time_zone) {
  36.  
  37. $this->time_zone = (string)$time_zone;
  38. return $this;
  39.  
  40. }
  41.  
  42. function __get($property) {
  43.  
  44. return $this->$property;
  45.  
  46. }
  47.  
  48. function logEvent() {
  49.  
  50. $my_file = $this->file_path;
  51. $fh = fopen($my_file, 'a') or die($this->failed_message);
  52. $string_data = "$this->event_message," . date('m/d/y,h:m:s,a,') . $this->time_zone . "\n";
  53. fwrite($fh, $string_data);
  54. fclose($fh);
  55.  
  56. return true;
  57.  
  58. }
  59. }
  60. //Usage
  61. $l = new Log_Event();
  62. $l->setEventMessage('Logging')
  63. ->setFilePath('file.csv')
  64. ->setFailedMessage('failed')
  65. ->setTimeZone('PST')
  66. ->logEvent();

Report this snippet  

You need to login to post a comment.