File.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. ];
  26. protected $writed = [];
  27. // 实例化并传入参数
  28. public function __construct($config = [])
  29. {
  30. if (is_array($config)) {
  31. $this->config = array_merge($this->config, $config);
  32. }
  33. }
  34. /**
  35. * 日志写入接口
  36. * @access public
  37. * @param array $log 日志信息
  38. * @return bool
  39. */
  40. public function save(array $log = [])
  41. {
  42. if ($this->config['single']) {
  43. $destination = $this->config['path'] . 'single.log';
  44. } else {
  45. $cli = IS_CLI ? '_cli' : '';
  46. $destination = $this->config['path'] . date('Ym') . DS . date('d') . $cli . '.log';
  47. }
  48. $path = dirname($destination);
  49. !is_dir($path) && mkdir($path, 0755, true);
  50. $info = '';
  51. foreach ($log as $type => $val) {
  52. $level = '';
  53. foreach ($val as $msg) {
  54. if (!is_string($msg)) {
  55. $msg = var_export($msg, true);
  56. }
  57. $level .= '[ ' . $type . ' ] ' . $msg . "\r\n";
  58. }
  59. if (in_array($type, $this->config['apart_level'])) {
  60. // 独立记录的日志级别
  61. if ($this->config['single']) {
  62. $filename = $path . DS . $type . '.log';
  63. } else {
  64. $filename = $path . DS . date('d') . '_' . $type . $cli . '.log';
  65. }
  66. $this->write($level, $filename, true);
  67. } else {
  68. $info .= $level;
  69. }
  70. }
  71. if ($info) {
  72. return $this->write($info, $destination);
  73. }
  74. return true;
  75. }
  76. protected function write($message, $destination, $apart = false)
  77. {
  78. //检测日志文件大小,超过配置大小则备份日志文件重新生成
  79. if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
  80. try {
  81. rename($destination, dirname($destination) . DS . time() . '-' . basename($destination));
  82. } catch (\Exception $e) {
  83. }
  84. $this->writed[$destination] = false;
  85. }
  86. if (empty($this->writed[$destination]) && !IS_CLI) {
  87. if (App::$debug && !$apart) {
  88. // 获取基本信息
  89. if (isset($_SERVER['HTTP_HOST'])) {
  90. $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  91. } else {
  92. $current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
  93. }
  94. $runtime = round(microtime(true) - THINK_START_TIME, 10);
  95. $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
  96. $time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
  97. $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
  98. $memory_str = ' [内存消耗:' . $memory_use . 'kb]';
  99. $file_load = ' [文件加载:' . count(get_included_files()) . ']';
  100. $message = '[ info ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n" . $message;
  101. }
  102. $now = date($this->config['time_format']);
  103. $ip = Request::instance()->ip();
  104. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI';
  105. $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
  106. $message = "---------------------------------------------------------------\r\n[{$now}] {$ip} {$method} {$uri}\r\n" . $message;
  107. $this->writed[$destination] = true;
  108. }
  109. if (IS_CLI) {
  110. $now = date($this->config['time_format']);
  111. $message = "[{$now}]" . $message;
  112. }
  113. return error_log($message, 3, $destination);
  114. }
  115. }