loger.php 965 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace plugins;
  3. class loger implements \Qii\Loger\Writer
  4. {
  5. public $logerPath = 'tmp';
  6. public $fileName = 'loger.log';
  7. public function __construct()
  8. {
  9. $this->logerPath = dirname(dirname(__FILE__)) .DS. $this->logerPath;
  10. $this->fileName = $this->logerPath . DS . date('Y-m-d') .'.'. $this->fileName;
  11. return $this;
  12. }
  13. public function setFileName($fileName)
  14. {
  15. $this->fileName = $this->logerPath . DS . date('Y-m-d') .'.'. $fileName . '.log';
  16. return $this;
  17. }
  18. protected function trimSpace($text)
  19. {
  20. return str_replace(array("\r\n", "\r", "\n", "\t", "\s", ' ', chr(32)), "", $text);
  21. }
  22. public function formatLog($log)
  23. {
  24. if(!is_array($log)) return $log;
  25. return json_encode($log);
  26. return $this->trimSpace(print_r($log, true));
  27. }
  28. public function writeLog($loger)
  29. {
  30. if(is_array($loger)) $loger = $this->formatLog($loger);
  31. file_put_contents($this->fileName, date('Y-m-d H:i:s') ."\n\t". $loger . "\n", FILE_APPEND);
  32. }
  33. }