TelegramHandler.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Log;
  3. use App\IpLocation;
  4. use App\Notification\TelegramBot;
  5. use Illuminate\Support\Facades\Crypt;
  6. use Monolog\Handler\AbstractProcessingHandler;
  7. use Monolog\Logger;
  8. class TelegramHandler extends AbstractProcessingHandler
  9. {
  10. /**
  11. * @var DingDingRobot
  12. */
  13. protected $telegramBot;
  14. public function __construct(TelegramBot $telegramBot, $level = Logger::DEBUG, $bubble = true)
  15. {
  16. parent::__construct($level, $bubble);
  17. $this->telegramBot = $telegramBot;
  18. }
  19. /**
  20. * Writes the record down to the log of the implementing handler
  21. *
  22. * @param array $record
  23. * @return void
  24. */
  25. protected function write(array $record)
  26. {
  27. $ip = IpLocation::getIP();
  28. $agent=@$_SERVER['HTTP_USER_AGENT']??"";
  29. $locale=@$_SERVER['HTTP_ACCEPT_LANGUAGE']??"";
  30. $ref = @$_SERVER['HTTP_REFERER'];
  31. $url = @$_SERVER['REQUEST_URI'];
  32. $host = @$_SERVER['HTTP_HOST'];
  33. $request=$_REQUEST;
  34. if(isset($_REQUEST['sign'])){
  35. try {
  36. $request['user'] = Crypt::decryptString($_REQUEST['sign']);
  37. unset($request['sign']);
  38. }catch (\Exception $e){
  39. }
  40. }
  41. $envs=json_encode(compact('ip','agent','locale','ref','url','host','request'),JSON_PRETTY_PRINT);
  42. $this->telegramBot->sendMsg(
  43. env('APP_ENV')."## [ERROR级别日志]\n$envs\n{$record['formatted']}"
  44. );
  45. // unset($record['formatted']);
  46. // $this->telegramBot->sendMsg(
  47. // json_encode($record, JSON_UNESCAPED_UNICODE)
  48. // );
  49. }
  50. }