DebugLazyLogger.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. class DebugLazyLogger {
  3. function __construct() {
  4. $this->_items = [];
  5. $prefix = ('production' == V::get('P5_ENV', 'production', $_SERVER)) ? "" : "dev-";
  6. $this->_logFileName = "/tmp/{$prefix}se-debug-" . implode('-', [
  7. date("Y-m-d"),
  8. User::getLogin(),
  9. Request::getUserIp(),
  10. substr(session_id(), 0, 6),
  11. V::get('REQUEST_TIME', '', $_SERVER)// [REQUEST_TIME] => 1485770466
  12. ]) . '.log';
  13. }
  14. static function getInstance() {
  15. static $instance;
  16. if (!$instance) $instance = new DebugLazyLogger();
  17. return $instance;
  18. }
  19. // DebugLazyLogger::log($mixedArg, $type, $msg, $logFileName);
  20. static function log($mixedArg, $type, $msg) {
  21. $logger = self::getInstance();
  22. $logger->addLog($mixedArg, $type, $msg);
  23. }
  24. function addLog($mixedArg, $type, $msg) {
  25. $microTime = date("Y-m-d H:i:s") . substr((string)microtime(), 1, 6);
  26. $this->_items[] = [ $microTime, $mixedArg, $type, $msg ];
  27. }
  28. function __destruct() {
  29. $startSaveLog = date("Y-m-d H:i:s") . substr((string)microtime(), 1, 6);
  30. $firstItem = reset($this->_items);
  31. foreach ($this->_items as $logItem) { // $logItem: [ $mixedArg, $type, $msg, $logFileName ]
  32. list($microTime, $mixedArg, $type, $msg) = $logItem;
  33. self::_log($microTime, $mixedArg, $type, $msg, $this->_logFileName);
  34. }
  35. $endSaveLog = date("Y-m-d H:i:s") . substr((string)microtime(), 1, 6);
  36. self::_log($startSaveLog, $mixedArg = null, $type = 'string', $msg = "Start saving log", $this->_logFileName);
  37. self::_log($endSaveLog, $mixedArg = null, $type = 'string', $msg = "End saving log", $this->_logFileName);
  38. self::_log($endSaveLog, $mixedArg = [
  39. "{$firstItem[0]} diff(-----) - first log entry",
  40. "{$startSaveLog} diff(" . V::milisecondsStringDiff($startSaveLog, $firstItem[0]) . ") - start saving log to file",
  41. "{$endSaveLog} diff(" . V::milisecondsStringDiff($endSaveLog, $startSaveLog) . ") - end saving log to file",
  42. ], $type = 'array', $msg = "Summary", $this->_logFileName);
  43. }
  44. public static function _log($microTime ,$mixedArg, $type, $msg, $logFileName) {
  45. $logInfo = [
  46. 'date' => $microTime,
  47. 'type' => $type,
  48. 'msg' => $msg,
  49. 'log' => '',
  50. 'trace' => '',
  51. ];
  52. if ($mixedArg instanceof Exception) {
  53. $logInfo['type'] = 'Exception';
  54. if (!empty($logInfo['msg'])) $logInfo['msg'] .= ". ";
  55. $logInfo['msg'] .= $mixedArg->getMessage();
  56. $logInfo['log'] = [
  57. 'code' => $mixedArg->getCode(),
  58. 'line' => $mixedArg->getLine(),
  59. 'file' => str_replace(APP_PATH_ROOT, 'SE', $mixedArg->getFile()),
  60. ];
  61. $logInfo['trace'] = $mixedArg->getTraceAsString();// getTrace
  62. } else if (is_string($mixedArg)) {
  63. if ('sql' == $type) {
  64. if (!$logInfo['type']) $logInfo['type'] = 'sql';
  65. if (empty($logInfo['msg'])) $logInfo['msg'] = "sql";
  66. $logInfo['log'] = $mixedArg;
  67. } else {
  68. if (empty($logInfo['type'])) $logInfo['type'] = 'string';
  69. if (empty($msg)) {
  70. $logInfo['msg'] = $mixedArg;
  71. $logInfo['log'] = '';
  72. } else {
  73. $logInfo['msg'] = $msg;
  74. $logInfo['log'] = $mixedArg;
  75. }
  76. }
  77. ob_start();
  78. debug_print_backtrace();
  79. $logInfo['trace'] = ob_get_clean();
  80. } else if ('array' == $type || is_array($mixedArg)) {
  81. $mixedArg = (array)$mixedArg;
  82. if (!$logInfo['type']) $logInfo['type'] = 'array';
  83. if (!empty($logInfo['msg']) && !empty($mixedArg['msg'])) $logInfo['msg'] .= ". {$mixedArg['msg']}";
  84. else if (empty($logInfo['msg']) && !empty($mixedArg['msg'])) $logInfo['msg'] = $mixedArg['msg'];
  85. if (!empty($mixedArg['msg'])) unset($mixedArg['msg']);
  86. $logInfo['log'] = $mixedArg;
  87. ob_start();
  88. debug_print_backtrace();
  89. $logInfo['trace'] = ob_get_clean();
  90. }
  91. if (!empty($logInfo['trace']) && 'Exception' !== $logInfo['type']) {// remove #0 and #1 (DBG::log and DBG::_log)
  92. $pos = strpos($logInfo['trace'], "\n#2");
  93. // if (false !== $pos) $logInfo['trace'] = substr($logInfo['trace'], $pos + 1); // TODO
  94. }
  95. $logInfo['trace'] = str_replace(APP_PATH_ROOT, 'SE', $logInfo['trace']);
  96. $logInfo['trace'] .= (("\n" == substr($logInfo['trace'], -1)) ? '' : "\n") . "#URI: " . V::get('REQUEST_URI', '', $_SERVER);
  97. if (!$logInfo['type']) $logInfo['type'] = 'unknown';
  98. if (!empty($logInfo['trace'])) {
  99. $trace = array_map(function ($part) {
  100. if ('URI: ' === substr($part, 0, strlen('URI: '))) return "#{$part}";
  101. if ($pos = strpos($part, '{closure}')) {
  102. // #10 Route_Storage_AclStruct->{closure}(Array ([namespace] => default_db/BI_audit_ENERGA_PRAC ... )
  103. return "#" . substr($part, 0, 200) . ( strlen($part) > 200 ? '...' : '' );
  104. }
  105. if ($pos = strrpos($part, ' called at [')) {
  106. $spacePos = strpos($part, ' ');
  107. $spacePos = (' ' === $part[$spacePos + 1]) ? $spacePos + 1 : $spacePos;
  108. $spacePos = (' ' === $part[$spacePos + 1]) ? $spacePos + 1 : $spacePos;
  109. $spacePos += 1;
  110. $called = substr($part, $pos + strlen(' called at ['), -1);
  111. $nr = substr($part, 0, $spacePos);
  112. $line = substr($part, $spacePos, ($pos > 200) ? 200 : $pos - $spacePos);
  113. if ($pos > 200) $line .= "...";
  114. $line = str_replace("\n", '\\n', $line);
  115. return "#{$nr}{$called}: {$line}";
  116. }
  117. return "#" . substr($part, 0, 200) . ( strlen($part) > 200 ? '...' : '' );
  118. }, explode("\n#", $logInfo['trace']));
  119. array_shift($trace);
  120. $logInfo['trace'] = implode("\n", $trace);
  121. }
  122. error_log(
  123. json_encode($logInfo) . "\n"
  124. , 3
  125. , $logFileName
  126. );
  127. }
  128. }