log.init.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 - 工具库-日
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class logInit {
  12. private $default_file_size = '1024000'; //默认日志文件大小
  13. /**
  14. * 写日志-直接写入日志文件或者邮件
  15. * 使用方法:$this->getUtil('log')->write('日志内容');
  16. * @param string $message 日志信息
  17. * @param string $log_type 日志类型 ERROR WARN DEBUG INFO
  18. * @return
  19. */
  20. public function write($message, $log_type = 'DEBUG') {
  21. $log_path = $this->get_file_log_name();
  22. if(is_file($log_path) && ($this->default_file_size < filesize($log_path)) ) {
  23. rename($log_path, dirname($log_path).'/'.time().'-Bak-'.basename($log_path));
  24. }
  25. $message = $this->get_message($message, $log_type);
  26. error_log($message, 3, $log_path, '');
  27. }
  28. /**
  29. * 写日志-获取文件日志名称
  30. * @return string
  31. */
  32. private function get_file_log_name() {
  33. $config = InitPHP::getConfig();
  34. return $config['log_dir'] . $this->_errorLogFileName();
  35. }
  36. /**
  37. * 写日志-组装message信息
  38. * @param string $message 日志信息
  39. * @param string $log_type 日志类型
  40. * @return string
  41. */
  42. private function get_message($message, $log_type) {
  43. return date("Y-m-d H:i:s") . " [{$log_type}] : {$message}\r\n";
  44. }
  45. /**
  46. *
  47. * @return string
  48. */
  49. private function _errorLogFileName(){
  50. return "initphp_log_" . date('Y-m-d').'.log';
  51. }
  52. }