Log.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Think;
  12. /**
  13. * 日志处理类
  14. */
  15. class Log {
  16. // 日志级别 从上到下,由低到高
  17. const EMERG = 'EMERG'; // 严重错误: 导致系统崩溃无法使用
  18. const ALERT = 'ALERT'; // 警戒性错误: 必须被立即修改的错误
  19. const CRIT = 'CRIT'; // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样
  20. const ERR = 'ERR'; // 一般错误: 一般性错误
  21. const WARN = 'WARN'; // 警告性错误: 需要发出警告的错误
  22. const NOTICE = 'NOTIC'; // 通知: 程序可以运行但是还不够完美的错误
  23. const INFO = 'INFO'; // 信息: 程序输出信息
  24. const DEBUG = 'DEBUG'; // 调试: 调试信息
  25. const SQL = 'SQL'; // SQL:SQL语句 注意只在调试模式开启时有效
  26. // 日志信息
  27. static protected $log = array();
  28. // 日志存储
  29. static protected $storage = null;
  30. // 日志初始化
  31. static public function init($config=array()){
  32. $type = isset($config['type']) ? $config['type'] : 'File';
  33. $class = strpos($type,'\\')? $type: 'Think\\Log\\Driver\\'. ucwords(strtolower($type));
  34. unset($config['type']);
  35. debug($config,'日志初始化');
  36. self::$storage = new $class($config);
  37. }
  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_LEVEL'),$level)) {
  49. debug("{$level}: {$message}\r\n",'log record');
  50. self::$log[] = "{$level}: {$message}\r\n";
  51. }
  52. }
  53. /**
  54. * 日志保存
  55. * @static
  56. * @access public
  57. * @param integer $type 日志记录方式
  58. * @param string $destination 写入目标
  59. * @return void
  60. */
  61. static function save($type='',$destination='') {
  62. if(empty(self::$log)) return ;
  63. if(empty($destination)){
  64. $destination = C('LOG_PATH').date('y_m_d').'.log';
  65. }
  66. if(!self::$storage){
  67. $type = $type ? : C('LOG_TYPE');
  68. $class = 'Think\\Log\\Driver\\'. ucwords($type);
  69. self::$storage = new $class();
  70. }
  71. $message = implode('',self::$log);
  72. debug('log save');
  73. self::$storage->write($message,$destination);
  74. // 保存后清空日志缓存
  75. self::$log = array();
  76. }
  77. /**
  78. * 日志直接写入
  79. * @static
  80. * @access public
  81. * @param string $message 日志信息
  82. * @param string $level 日志级别
  83. * @param integer $type 日志记录方式
  84. * @param string $destination 写入目标
  85. * @return void
  86. */
  87. static function write($message,$level=self::ERR,$type='',$destination='') {
  88. if(!self::$storage){
  89. $type = $type ? : C('LOG_TYPE');
  90. $class = 'Think\\Log\\Driver\\'. ucwords($type);
  91. $config['log_path'] = C('LOG_PATH');
  92. self::$storage = new $class($config);
  93. }
  94. if(empty($destination)){
  95. $destination = C('LOG_PATH').date('y_m_d').'.log';
  96. }
  97. debug("{$level}: {$message}, $destination",'log write');
  98. self::$storage->write("{$level}: {$message}", $destination);
  99. }
  100. }