PushLogConfig.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. if (empty($logLevel)) {
  47. $logLevel = Constants::HW_PUSH_LOG_INFO_LEVEL;
  48. }
  49. if ($logLevel > $this->default_log_level) {
  50. return;
  51. }
  52. date_default_timezone_set($timeZone);
  53. $time = strftime($timeFormat, time());
  54. $msg = str_replace("\t", '', $msg);
  55. $msg = str_replace("\n", '', $msg);
  56. $strLogLevel = $this->levelToString($logLevel);
  57. if (isset($module)) {
  58. $module = '[' . str_pad(str_replace(array(
  59. "\n",
  60. "\t"
  61. ), array(
  62. "",
  63. ""
  64. ), $module), $this->LOG_MODULE_FIXED_LEN) . ']';
  65. $logLine = "$time\t$strLogLevel\t$module\t$msg\r\n";
  66. } else {
  67. $logLine = "$time\t$strLogLevel\t$msg\r\n";
  68. }
  69. print_r($logLine . '<br>');
  70. fwrite($this->LogFile, $logLine);
  71. }
  72. private function levelToString($logLevel)
  73. {
  74. $ret = 'LOG::UNKNOWN';
  75. switch ($logLevel) {
  76. case Constants::HW_PUSH_LOG_DEBUG_LEVEL:
  77. $ret = 'LOG::DEBUG';
  78. break;
  79. case Constants::HW_PUSH_LOG_INFO_LEVEL:
  80. $ret = 'LOG::INFO';
  81. break;
  82. case Constants::HW_PUSH_LOG_WARN_LEVEL:
  83. $ret = 'LOG::WARNING';
  84. break;
  85. case Constants::HW_PUSH_LOG_ERROR_LEVEL:
  86. $ret = 'LOG::ERROR';
  87. break;
  88. }
  89. return $ret;
  90. }
  91. }