logger.php 1.0 KB

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