File.class.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://topthink.com 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. class File
  13. {
  14. protected $config = array(
  15. 'log_time_format' => ' c ',
  16. 'log_file_size' => 2097152,
  17. 'log_path' => '',
  18. );
  19. // 实例化并传入参数
  20. public function __construct($config = array())
  21. {
  22. $this->config = array_merge($this->config, $config);
  23. }
  24. /**
  25. * 日志写入接口
  26. * @access public
  27. * @param string $log 日志信息
  28. * @param string $destination 写入目标
  29. * @return void
  30. */
  31. public function write($log, $destination = '')
  32. {
  33. $now = date($this->config['log_time_format']);
  34. if (empty($destination)) {
  35. $destination = $this->config['log_path'] . date('y_m_d') . '.log';
  36. }
  37. // 自动创建日志目录
  38. $log_dir = dirname($destination);
  39. if (!is_dir($log_dir)) {
  40. mkdir($log_dir, 0755, true);
  41. }
  42. //检测日志文件大小,超过配置大小则备份日志文件重新生成
  43. if (is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination)) {
  44. rename($destination, dirname($destination) . '/' . time() . '-' . basename($destination));
  45. }
  46. error_log("[{$now}] " . $_SERVER['REMOTE_ADDR'] . ' ' . $_SERVER['REQUEST_URI'] . "\r\n{$log}\r\n", 3, $destination);
  47. }
  48. }