logger.php 5.1 KB

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