/google_api/Logger/Psr.php

  1. <?php 
  2. /** 
  3. * Copyright 2014 Google Inc. 
  4. * 
  5. * Licensed under the Apache License, Version 2.0 (the "License"); 
  6. * you may not use this file except in compliance with the License. 
  7. * You may obtain a copy of the License at 
  8. * 
  9. * http://www.apache.org/licenses/LICENSE-2.0 
  10. * 
  11. * Unless required by applicable law or agreed to in writing, software 
  12. * distributed under the License is distributed on an "AS IS" BASIS,  
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14. * See the License for the specific language governing permissions and 
  15. * limitations under the License. 
  16. */ 
  17.  
  18. if (!class_exists('Google_Client')) { 
  19. require_once dirname(__FILE__) . '/../autoload.php'; 
  20.  
  21. /** 
  22. * Psr logging class based on the PSR-3 standard. 
  23. * 
  24. * This logger will delegate all logging to a PSR-3 compatible logger specified 
  25. * with the `Google_Logger_Psr::setLogger()` method. 
  26. */ 
  27. class Google_Logger_Psr extends Google_Logger_Abstract 
  28. /** 
  29. * @param Psr\Log\LoggerInterface $logger The PSR-3 logger 
  30. */ 
  31. private $logger; 
  32.  
  33. /** 
  34. * @param Google_Client $client The current Google client 
  35. * @param Psr\Log\LoggerInterface $logger PSR-3 logger where logging will be delegated. 
  36. */ 
  37. public function __construct(Google_Client $client, /**Psr\Log\LoggerInterface*/ $logger = null) 
  38. parent::__construct($client); 
  39.  
  40. if ($logger) { 
  41. $this->setLogger($logger); 
  42.  
  43. /** 
  44. * Sets the PSR-3 logger where logging will be delegated. 
  45. * 
  46. * NOTE: The `$logger` should technically implement 
  47. * `Psr\Log\LoggerInterface`, but we don't explicitly require this so that 
  48. * we can be compatible with PHP 5.2. 
  49. * 
  50. * @param Psr\Log\LoggerInterface $logger The PSR-3 logger 
  51. */ 
  52. public function setLogger(/**Psr\Log\LoggerInterface*/ $logger) 
  53. $this->logger = $logger; 
  54.  
  55. /** 
  56. * {@inheritdoc} 
  57. */ 
  58. public function shouldHandle($level) 
  59. return isset($this->logger) && parent::shouldHandle($level); 
  60.  
  61. /** 
  62. * {@inheritdoc} 
  63. */ 
  64. public function log($level, $message, array $context = array()) 
  65. if (!$this->shouldHandle($level)) { 
  66. return false; 
  67.  
  68. if ($context) { 
  69. $this->reverseJsonInContext($context); 
  70.  
  71. $levelName = is_int($level) ? array_search($level, self::$levels) : $level; 
  72. $this->logger->log($levelName, $message, $context); 
  73.  
  74. /** 
  75. * {@inheritdoc} 
  76. */ 
  77. protected function write($message, array $context = array()) 
.