Log.php 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Spider\Lib;
  3. use Dever;
  4. class Log
  5. {
  6. private $time = 0;
  7. private $file;
  8. private $content = array();
  9. public function __construct($key)
  10. {
  11. $key = date('Y-m-d') . '_' . $key;
  12. $this->file = Dever::path(Dever::data() . 'logs/', 'spider/' . $key);
  13. }
  14. public function get()
  15. {
  16. $content = file_get_contents($this->file);
  17. return $content;
  18. }
  19. public function add($string)
  20. {
  21. $time = Dever::msectime();
  22. if ($this->time == 0) {
  23. $hs = 0;
  24. $this->time = $time;
  25. } else {
  26. $hs = $time - $this->time;
  27. }
  28. $time = date('Y-m-d H:i:s');
  29. $content = array
  30. (
  31. '时间:' . $time,
  32. '耗时:' . $hs . 'MS',
  33. '内容:' . str_replace("\n", '<--', $string),
  34. );
  35. $this->content[] = implode(' ', $content);
  36. }
  37. public function out()
  38. {
  39. print_r(Dever::table($this->content));
  40. }
  41. public function save()
  42. {
  43. $content = implode("\r\n", $this->content);
  44. file_put_contents($this->file, $content, FILE_APPEND);
  45. }
  46. }