PushLogConfig.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin;
  3. use Exception;
  4. class PushLogConfig
  5. {
  6. private $LogFile;
  7. private $logBanner = "
  8. ___ ___ __ __ __________ ____ ___ _________ ___ ___ __________ ___ _____________
  9. / | \/ \ / \ \______ \ | \/ _____// | \ \______ \/ | \______ \
  10. / ~ \ \/\/ / | ___/ | /\_____ \/ ~ \ | ___/ ~ \ ___/
  11. \ Y /\ / | | | | / / \ Y / | | \ Y / |
  12. \___|_ / \__/\ / |____| |______/ /_______ /\___|_ / |____| \___|_ /|____|
  13. \/ \/ \/ \/ \/
  14. ";
  15. private $LOG_MODULE_FIXED_LEN = 20;
  16. private $default_log_level = Constants::HW_PUSH_LOG_INFO_LEVEL;
  17. private function __construct()
  18. {
  19. // $this->LogFile = @fopen(Constants::HW_PUSH_LOG_FILE_NAME, 'a+');
  20. // if (!is_resource($this->LogFile)) {
  21. // throw new Exception(Constants::HW_PUSH_LOG_FILE_NAME . 'invalid file Stream');
  22. // }
  23. // fwrite($this->LogFile, $this->logBanner);
  24. $pushConfig = PushConfig::getSingleInstance();
  25. // $this->default_log_level = $pushConfig->HW_DEFAULT_LOG_LEVEL;
  26. // if (empty($this->default_log_level)) {
  27. // $this->default_log_level = Constants::HW_PUSH_LOG_INFO_LEVEL;
  28. // }
  29. }
  30. /**
  31. * single instance
  32. */
  33. public static function getSingleInstance()
  34. {
  35. static $obj;
  36. if (!isset($obj)) {
  37. $obj = new PushLogConfig();
  38. }
  39. return $obj;
  40. }
  41. /**
  42. * core log process
  43. */
  44. public function LogMessage($msg, $logLevel = Constants::HW_PUSH_LOG_INFO_LEVEL, $module = null, $timeZone = 'Asia/shanghai', $timeFormat = "%Y-%m-%d %H:%M:%S")
  45. {
  46. return;
  47. if (empty($logLevel)) {
  48. $logLevel = Constants::HW_PUSH_LOG_INFO_LEVEL;
  49. }
  50. if ($logLevel > $this->default_log_level) {
  51. return;
  52. }
  53. date_default_timezone_set($timeZone);
  54. $time = strftime($timeFormat, time());
  55. $msg = str_replace("\t", '', $msg);
  56. $msg = str_replace("\n", '', $msg);
  57. $strLogLevel = $this->levelToString($logLevel);
  58. if (isset($module)) {
  59. $module = '[' . str_pad(str_replace(array(
  60. "\n",
  61. "\t"
  62. ), array(
  63. "",
  64. ""
  65. ), $module), $this->LOG_MODULE_FIXED_LEN) . ']';
  66. $logLine = "$time\t$strLogLevel\t$module\t$msg\r\n";
  67. } else {
  68. $logLine = "$time\t$strLogLevel\t$msg\r\n";
  69. }
  70. print_r($logLine . '<br>');
  71. fwrite($this->LogFile, $logLine);
  72. }
  73. private function levelToString($logLevel)
  74. {
  75. $ret = 'LOG::UNKNOWN';
  76. switch ($logLevel) {
  77. case Constants::HW_PUSH_LOG_DEBUG_LEVEL:
  78. $ret = 'LOG::DEBUG';
  79. break;
  80. case Constants::HW_PUSH_LOG_INFO_LEVEL:
  81. $ret = 'LOG::INFO';
  82. break;
  83. case Constants::HW_PUSH_LOG_WARN_LEVEL:
  84. $ret = 'LOG::WARNING';
  85. break;
  86. case Constants::HW_PUSH_LOG_ERROR_LEVEL:
  87. $ret = 'LOG::ERROR';
  88. break;
  89. }
  90. return $ret;
  91. }
  92. }