You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

166 lines
4.0 KiB

  1. <?php
  2. /**
  3. * *************************************************************************
  4. *
  5. * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  6. *
  7. * ************************************************************************
  8. */
  9. /**
  10. *
  11. * @file SimpleLog.php
  12. * @encoding UTF-8
  13. *
  14. *
  15. * @date 2014年12月25日
  16. *
  17. */
  18. class PushSimpleLog {
  19. private $out = null;
  20. private $log_level = 1;
  21. private $max_level = 3;
  22. private $prefix = 'PUSH_SDK';
  23. private $tags = array (
  24. 'DEV',
  25. 'INFO',
  26. 'WARN',
  27. 'FAULT',
  28. );
  29. /**
  30. * 实际log输出方法
  31. *
  32. * @param mixed $log
  33. * @param number $level
  34. * @param string $prefix
  35. * @return void
  36. */
  37. protected function innerWrite($log, $level = 1, $prefix = null) {
  38. $level = intval($level);
  39. if(intval($level) < $this->log_level || $this -> out === null){
  40. return;
  41. }
  42. $timestamp = time();
  43. $prefix = $prefix === null ? $this->prefix : $prefix;
  44. $tag = $this->tags[$level];
  45. $msg = "[$tag][$timestamp][$prefix] $log; \n";
  46. if (is_callable($this -> out)) {
  47. $out = $this -> out;
  48. $out($msg . '<br />'); // if out to page , add the BR, easy to watch
  49. return;
  50. }
  51. fwrite($this -> out, $msg);
  52. }
  53. /**
  54. * 打印一条dev信息
  55. * @param string $log
  56. * @param string $prefix
  57. */
  58. function dev($log, $prefix = null) {
  59. $this -> innerWrite($log, 0, $prefix);
  60. }
  61. /**
  62. * 打印一条info信息
  63. * @param string $log
  64. * @param string $prefix
  65. */
  66. function info($log, $prefix = null) {
  67. $this -> innerWrite($log, 1, $prefix);
  68. }
  69. /**
  70. * 打印一条warn信息
  71. * @param string $log
  72. * @param string $prefix
  73. */
  74. function warn($log, $prefix = null) {
  75. $this -> innerWrite($log, 2, $prefix);
  76. }
  77. /**
  78. * 打印一条fault信息
  79. * @param string $log
  80. * @param string $prefix
  81. */
  82. function fault($log, $prefix = null) {
  83. $this -> innerWrite($log, 3, $prefix);
  84. }
  85. // alias方法
  86. /**
  87. * alias: dev
  88. * @param string $log
  89. * @param string $prefix
  90. */
  91. function debug($log, $prefix = null) {
  92. $this -> dev($log, $prefix);
  93. }
  94. /**
  95. * alias: info
  96. * @param string $log
  97. * @param string $prefix
  98. */
  99. function log($log, $prefix = null) {
  100. $this -> info($log, $prefix);
  101. }
  102. /**
  103. * alias: fault
  104. * @param string $log
  105. * @param string $prefix
  106. */
  107. function error($log, $prefix = null) {
  108. $this -> fault($log, $prefix);
  109. }
  110. /**
  111. * alias: fault
  112. * @param string $log
  113. * @param string $prefix
  114. */
  115. function err($log, $prefix = null) {
  116. $this -> fault($log, $prefix);
  117. }
  118. /**
  119. * Constructor
  120. * @param string $out 输出位置, 默认为disable即禁用.
  121. * @param number $level 过滤级别, 0 - 4, 数值越小信息越详细, 4则不打印任何信息
  122. * @return void
  123. */
  124. function __construct($out = 'disable', $level = 1) {
  125. if($level > $this -> max_level){
  126. $out = 'disable';
  127. }
  128. switch ($out) {
  129. case "disable" :
  130. // just ignore !!
  131. return;
  132. case "page" :
  133. $this -> out = 'print_r';
  134. break;
  135. case "stdout" :
  136. $out = "php://stdout";
  137. $this -> out = fopen($out, 'w');
  138. break;
  139. default :
  140. // if can not be write , log will be disable!
  141. if (is_writable($out)) {
  142. $this -> out = fopen($out, 'w');
  143. }
  144. }
  145. $this->log_level = $level;
  146. $this->log("PushSimpleLog: ready to work!");
  147. }
  148. /**
  149. * __destruct
  150. */
  151. function __destruct(){
  152. @is_resource($this->out) ? fclose($this->out) : $this->out = null;
  153. }
  154. }