Log.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * 日志处理类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Core
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Log extends Think
  24. {//类定义开始
  25. // 日志级别 从上到下,由低到高
  26. const EMERG = 'EMERG'; // 严重错误: 导致系统崩溃无法使用
  27. const ALERT = 'ALERT'; // 警戒性错误: 必须被立即修改的错误
  28. const CRIT = 'CRIT'; // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样
  29. const ERR = 'ERR'; // 一般错误: 一般性错误
  30. const WARN = 'WARN'; // 警告性错误: 需要发出警告的错误
  31. const NOTICE = 'NOTIC'; // 通知: 程序可以运行但是还不够完美的错误
  32. const INFO = 'INFO'; // 信息: 程序输出信息
  33. const DEBUG = 'DEBUG'; // 调试: 调试信息
  34. const SQL = 'SQL'; // SQL:SQL语句 注意只在调试模式开启时有效
  35. // 日志记录方式
  36. const SYSTEM = 0;
  37. const MAIL = 1;
  38. const TCP = 2;
  39. const FILE = 3;
  40. // 日志信息
  41. static $log = array();
  42. // 日期格式
  43. static $format = '[ c ]';
  44. /**
  45. +----------------------------------------------------------
  46. * 记录日志 并且会过滤未经设置的级别
  47. +----------------------------------------------------------
  48. * @static
  49. * @access public
  50. +----------------------------------------------------------
  51. * @param string $message 日志信息
  52. * @param string $level 日志级别
  53. * @param boolean $record 是否强制记录
  54. +----------------------------------------------------------
  55. * @return void
  56. +----------------------------------------------------------
  57. */
  58. static function record($message,$level=self::ERR,$record=false) {
  59. if($record || in_array($level,C('LOG_RECORD_LEVEL'))) {
  60. $now = date(self::$format);
  61. self::$log[] = "{$now} {$level}: {$message}\r\n";
  62. }
  63. }
  64. /**
  65. +----------------------------------------------------------
  66. * 日志保存
  67. +----------------------------------------------------------
  68. * @static
  69. * @access public
  70. +----------------------------------------------------------
  71. * @param integer $type 日志记录方式
  72. * @param string $destination 写入目标
  73. * @param string $extra 额外参数
  74. +----------------------------------------------------------
  75. * @return void
  76. +----------------------------------------------------------
  77. */
  78. static function save($type=self::FILE,$destination='',$extra='')
  79. {
  80. if(empty($destination))
  81. $destination = LOG_PATH.date('y_m_d').".log";
  82. if(self::FILE == $type) { // 文件方式记录日志信息
  83. //检测日志文件大小,超过配置大小则备份日志文件重新生成
  84. if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
  85. rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
  86. }
  87. error_log(implode("",self::$log), $type,$destination ,$extra);
  88. // 保存后清空日志缓存
  89. self::$log = array();
  90. //clearstatcache();
  91. }
  92. /**
  93. +----------------------------------------------------------
  94. * 日志直接写入
  95. +----------------------------------------------------------
  96. * @static
  97. * @access public
  98. +----------------------------------------------------------
  99. * @param string $message 日志信息
  100. * @param string $level 日志级别
  101. * @param integer $type 日志记录方式
  102. * @param string $destination 写入目标
  103. * @param string $extra 额外参数
  104. +----------------------------------------------------------
  105. * @return void
  106. +----------------------------------------------------------
  107. */
  108. static function write($message,$level=self::ERR,$type=self::FILE,$destination='',$extra='')
  109. {
  110. $now = date(self::$format);
  111. if(empty($destination))
  112. $destination = LOG_PATH.date('y_m_d').".log";
  113. if(self::FILE == $type) { // 文件方式记录日志
  114. //检测日志文件大小,超过配置大小则备份日志文件重新生成
  115. if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
  116. rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
  117. }
  118. error_log("{$now} {$level}: {$message}\r\n", $type,$destination,$extra );
  119. //clearstatcache();
  120. }
  121. }//类定义结束
  122. ?>