File.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\log\driver;
  12. use think\App;
  13. use think\Request;
  14. /**
  15. * 本地化调试输出到文件
  16. */
  17. class File
  18. {
  19. protected $config = [
  20. 'time_format' => ' c ',
  21. 'single' => false,
  22. 'file_size' => 2097152,
  23. 'path' => LOG_PATH,
  24. 'apart_level' => [],
  25. 'max_files' => 0,
  26. ];
  27. protected $writed = [];
  28. // 实例化并传入参数
  29. public function __construct($config = [])
  30. {
  31. if (is_array($config)) {
  32. $this->config = array_merge($this->config, $config);
  33. }
  34. }
  35. /**
  36. * 日志写入接口
  37. * @access public
  38. * @param array $log 日志信息
  39. * @return bool
  40. */
  41. public function save(array $log = [])
  42. {
  43. if ($this->config['single']) {
  44. $destination = $this->config['path'] . 'single.log';
  45. } else {
  46. $cli = IS_CLI ? '_cli' : '';
  47. if ($this->config['max_files']) {
  48. $filename = date('Ymd') . $cli . '.log';
  49. $files = glob($this->config['path'] . '*.log');
  50. if (count($files) > $this->config['max_files']) {
  51. unlink($files[0]);
  52. }
  53. } else {
  54. $filename = date('Ym') . '/' . date('d') . $cli . '.log';
  55. }
  56. $destination = $this->config['path'] . $filename;
  57. }
  58. $path = dirname($destination);
  59. !is_dir($path) && mkdir($path, 0755, true);
  60. $info = '';
  61. foreach ($log as $type => $val) {
  62. $level = '';
  63. foreach ($val as $msg) {
  64. if (!is_string($msg)) {
  65. $msg = var_export($msg, true);
  66. }
  67. $level .= '[ ' . $type . ' ] ' . $msg . "\r\n";
  68. }
  69. if (in_array($type, $this->config['apart_level'])) {
  70. // 独立记录的日志级别
  71. if ($this->config['single']) {
  72. $filename = $path . DS . $type . '.log';
  73. } elseif ($this->config['max_files']) {
  74. $filename = $path . DS . date('Ymd') . '_' . $type . $cli . '.log';
  75. } else {
  76. $filename = $path . DS . date('d') . '_' . $type . $cli . '.log';
  77. }
  78. $this->write($level, $filename, true);
  79. } else {
  80. $info .= $level;
  81. }
  82. }
  83. if ($info) {
  84. return $this->write($info, $destination);
  85. }
  86. return true;
  87. }
  88. protected function write($message, $destination, $apart = false)
  89. {
  90. //检测日志文件大小,超过配置大小则备份日志文件重新生成
  91. if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
  92. try {
  93. rename($destination, dirname($destination) . DS . time() . '-' . basename($destination));
  94. } catch (\Exception $e) {
  95. }
  96. $this->writed[$destination] = false;
  97. }
  98. if (empty($this->writed[$destination]) && !IS_CLI) {
  99. if (App::$debug && !$apart) {
  100. // 获取基本信息
  101. if (isset($_SERVER['HTTP_HOST'])) {
  102. $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  103. } else {
  104. $current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
  105. }
  106. $runtime = round(microtime(true) - THINK_START_TIME, 10);
  107. $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
  108. $time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
  109. $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
  110. $memory_str = ' [内存消耗:' . $memory_use . 'kb]';
  111. $file_load = ' [文件加载:' . count(get_included_files()) . ']';
  112. $message = '[ info ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n" . $message;
  113. }
  114. $now = date($this->config['time_format']);
  115. $ip = Request::instance()->ip();
  116. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI';
  117. $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
  118. $message = "---------------------------------------------------------------\r\n[{$now}] {$ip} {$method} {$uri}\r\n" . $message;
  119. $this->writed[$destination] = true;
  120. }
  121. if (IS_CLI) {
  122. $now = date($this->config['time_format']);
  123. $message = "[{$now}]" . $message;
  124. }
  125. return error_log($message, 3, $destination);
  126. }
  127. }