File.php 4.0 KB

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