Log.class.php 4.1 KB

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