Logger.php 770 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. class LtLogger
  3. {
  4. public $conf = array(
  5. "separator" => "\t",
  6. "log_file" => ""
  7. );
  8. private $fileHandle;
  9. protected function getFileHandle()
  10. {
  11. if (null === $this->fileHandle)
  12. {
  13. if (empty($this->conf["log_file"]))
  14. {
  15. trigger_error("no log file spcified.");
  16. }
  17. $logDir = dirname($this->conf["log_file"]);
  18. if (!is_dir($logDir))
  19. {
  20. mkdir($logDir, 0777, true);
  21. }
  22. $this->fileHandle = fopen($this->conf["log_file"], "a");
  23. }
  24. return $this->fileHandle;
  25. }
  26. public function log($logData)
  27. {
  28. if ("" == $logData || array() == $logData)
  29. {
  30. return false;
  31. }
  32. if (is_array($logData))
  33. {
  34. $logData = implode($this->conf["separator"], $logData);
  35. }
  36. $logData = $logData. "\n";
  37. fwrite($this->getFileHandle(), $logData);
  38. }
  39. }