Log.class.php 3.9 KB

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