File.php 4.5 KB

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