Log.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +----------------------------------------------------------------------
  8. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  9. // +----------------------------------------------------------------------
  10. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  11. // +----------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +----------------------------------------------------------------------
  14. // | Author: liu21st <liu21st@gmail.com>
  15. // +----------------------------------------------------------------------
  16. namespace Think;
  17. /**
  18. * 日志处理类
  19. */
  20. class Log {
  21. // 日志级别 从上到下,由低到高
  22. const EMERG = 'EMERG'; // 严重错误: 导致系统崩溃无法使用
  23. const ALERT = 'ALERT'; // 警戒性错误: 必须被立即修改的错误
  24. const CRIT = 'CRIT'; // 临界值错误: 超过临界值的错误,例如一天24小时,而输入的是25小时这样
  25. const ERR = 'ERR'; // 一般错误: 一般性错误
  26. const WARN = 'WARN'; // 警告性错误: 需要发出警告的错误
  27. const NOTICE = 'NOTIC'; // 通知: 程序可以运行但是还不够完美的错误
  28. const INFO = 'INFO'; // 信息: 程序输出信息
  29. const DEBUG = 'DEBUG'; // 调试: 调试信息
  30. const SQL = 'SQL'; // SQL:SQL语句 注意只在调试模式开启时有效
  31. // 日志信息
  32. static protected $log = array();
  33. // 日志存储
  34. static protected $storage = null;
  35. // 日志初始化
  36. static public function init($config=array()){
  37. $type = isset($config['type'])?$config['type']:'File';
  38. $class = strpos($type,'\\')? $type: 'Think\\Log\\Driver\\'. ucwords(strtolower($type));
  39. unset($config['type']);
  40. self::$storage = new $class($config);
  41. }
  42. /**
  43. * 记录日志 并且会过滤未经设置的级别
  44. * @static
  45. * @access public
  46. * @param string $message 日志信息
  47. * @param string $level 日志级别
  48. * @param boolean $record 是否强制记录
  49. * @return void
  50. */
  51. static function record($message,$level=self::ERR,$record=false) {
  52. if($record || false !== strpos(C('LOG_LEVEL'),$level)) {
  53. self::$log[] = "{$level}: {$message}\r\n";
  54. }
  55. }
  56. /**
  57. * 日志保存
  58. * @static
  59. * @access public
  60. * @param integer $type 日志记录方式
  61. * @param string $destination 写入目标
  62. * @return void
  63. */
  64. static function save($type='',$destination='') {
  65. if(empty(self::$log)) return ;
  66. if(empty($destination))
  67. $destination = C('LOG_PATH').date('y_m_d').'.log';
  68. if(!self::$storage){
  69. $type = $type?:C('LOG_TYPE');
  70. $class = 'Think\\Log\\Driver\\'. ucwords($type);
  71. self::$storage = new $class();
  72. }
  73. $message = implode('',self::$log);
  74. self::$storage->write($message,$destination);
  75. // 保存后清空日志缓存
  76. self::$log = array();
  77. }
  78. /**
  79. * 日志直接写入
  80. * @static
  81. * @access public
  82. * @param string $message 日志信息
  83. * @param string $level 日志级别
  84. * @param integer $type 日志记录方式
  85. * @param string $destination 写入目标
  86. * @return void
  87. */
  88. static function write($message,$level=self::ERR,$type='',$destination='') {
  89. if(!self::$storage){
  90. $type = $type?:C('LOG_TYPE');
  91. $class = 'Think\\Log\\Driver\\'. ucwords($type);
  92. self::$storage = new $class();
  93. }
  94. if(empty($destination))
  95. $destination = C('LOG_PATH').date('y_m_d').'.log';
  96. self::$storage->write("{$level}: {$message}", $destination);
  97. }
  98. }