Wlog.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/5/22
  6. * Time: 11:07
  7. */
  8. namespace app\lib;
  9. use app\lib\GlobConfigs as GlobConfigs;
  10. use \Swoole\Async ;
  11. class Wlog
  12. {
  13. private static $inCrVel = 0;
  14. private static $Instance = null;
  15. private $redis = null;
  16. private $redis_ctime = 0;
  17. public static function getInstance($cache = true)
  18. {
  19. if ($cache && self::$Instance) {
  20. return self::$Instance;
  21. }
  22. self::$Instance = new self();
  23. return self::$Instance;
  24. }
  25. public function GetRedis()
  26. {
  27. $now = time();
  28. if ($now - $this->redis_ctime > 60 * 15) {
  29. if ($this->redis) {
  30. $this->redis->close();
  31. $this->redis = false;
  32. $this->redis_ctime = 0 ;
  33. }
  34. }
  35. if ($this->redis) {
  36. return $this->redis;
  37. }
  38. echo "新建一个Wlog->Coroutine_redis ".date("Y-m-d H:i:s")."\n";
  39. $config = GlobConfigs::getKey('redis');
  40. $redis = new \Swoole\Coroutine\Redis();
  41. $ret = $redis->connect($config['host'], $config['port']);
  42. if ($ret) {
  43. $this->redis = $redis;
  44. $this->redis_ctime = time();
  45. } else {
  46. $this->redis = false;
  47. }
  48. return $redis;
  49. }
  50. public function WriteLog($msg, $type = 3, $worker_id = 0)
  51. {
  52. $mself= $this;
  53. go(function()use ($msg,$type,$worker_id,$mself){
  54. $msg = json_encode([date("Y-m-d H:i:s"), $msg], 256);
  55. if (in_array($type, [1, 2])) {
  56. $redis = $mself->GetRedis();
  57. if ($redis) {
  58. if ($type == 1) {
  59. $minit = date("i");
  60. $ktype = RUN_LOGS_OVTIME_KEY . '_' . $minit;
  61. $key = 'Log' . ":" . $worker_id . '_' . $this->IncrKey();
  62. $otime = RUN_LOGS_OVTIME;
  63. $redis->hset($ktype, $key, $msg);
  64. $redis->expire($ktype, $otime);
  65. } else {
  66. $ktype = RUN_LOGS_OVTIME_KEY;
  67. $redis->lpush($ktype, $msg);
  68. }
  69. return true;
  70. }
  71. }
  72. //redis 失败时,写到日志文件中
  73. $fileName = date("Y-m-d") . '.log';
  74. $fullName = LOG_PATH . DS . $fileName;
  75. //\swoole_async_writefile($fullName, $msg . "\r\n", null, FILE_APPEND);
  76. //\Swoole\Async::writeFile($fullName, $msg . "\r\n", null, FILE_APPEND);
  77. file_put_contents($fullName,$msg."\r\n",FILE_APPEND);
  78. return true;
  79. });
  80. /*
  81. $msg = json_encode([date("Y-m-d H:i:s"), $msg], 256);
  82. if (in_array($type, [1, 2])) {
  83. $redis = $this->GetRedis();
  84. if ($redis) {
  85. if ($type == 1) {
  86. $minit = date("i");
  87. $ktype = RUN_LOGS_OVTIME_KEY . '_' . $minit;
  88. $key = $ktype . ":" . $worker_id . '_' . $this->IncrKey();
  89. $otime = RUN_LOGS_OVTIME;
  90. $redis->hset($ktype, $key, $msg);
  91. $redis->expire($ktype, $otime);
  92. } else {
  93. $ktype = RUN_LOGS_OVTIME_KEY;
  94. $redis->lpush($ktype, $msg);
  95. }
  96. return true;
  97. }
  98. }
  99. //redis 失败时,写到日志文件中
  100. $fileName = date("Y-m-d") . '.log';
  101. $fullName = LOG_PATH . DS . $fileName;
  102. swoole_async_writefile($fullName, $msg . "\r\n", null, FILE_APPEND);
  103. return true;
  104. */
  105. }
  106. public function IncrKey()
  107. {
  108. self::$inCrVel++;
  109. if (self::$inCrVel >= 99999999) {
  110. self::$inCrVel = 1;
  111. }
  112. return self::$inCrVel;
  113. }
  114. }