1<?php 2 3/** 4 * @see https://github.com/laminas/laminas-log for the canonical source repository 5 * @copyright https://github.com/laminas/laminas-log/blob/master/COPYRIGHT.md 6 * @license https://github.com/laminas/laminas-log/blob/master/LICENSE.md New BSD License 7 */ 8 9namespace Laminas\Log\Writer; 10 11use Laminas\Log\Exception; 12use Laminas\Log\Formatter\ChromePhp as ChromePhpFormatter; 13use Laminas\Log\Logger; 14use Laminas\Log\Writer\ChromePhp\ChromePhpBridge; 15use Laminas\Log\Writer\ChromePhp\ChromePhpInterface; 16use Traversable; 17 18class ChromePhp extends AbstractWriter 19{ 20 /** 21 * The instance of ChromePhpInterface that is used to log messages to. 22 * 23 * @var ChromePhpInterface 24 */ 25 protected $chromephp; 26 27 /** 28 * Initializes a new instance of this class. 29 * 30 * @param null|ChromePhpInterface|array|Traversable $instance An instance of ChromePhpInterface 31 * that should be used for logging 32 */ 33 public function __construct($instance = null) 34 { 35 if ($instance instanceof Traversable) { 36 $instance = iterator_to_array($instance); 37 } 38 39 if (is_array($instance)) { 40 parent::__construct($instance); 41 $instance = isset($instance['instance']) ? $instance['instance'] : null; 42 } 43 44 if (! ($instance instanceof ChromePhpInterface || $instance === null)) { 45 throw new Exception\InvalidArgumentException( 46 'You must pass a valid Laminas\Log\Writer\ChromePhp\ChromePhpInterface' 47 ); 48 } 49 50 $this->chromephp = $instance === null ? $this->getChromePhp() : $instance; 51 $this->formatter = new ChromePhpFormatter(); 52 } 53 54 /** 55 * Write a message to the log. 56 * 57 * @param array $event event data 58 * @return void 59 */ 60 protected function doWrite(array $event) 61 { 62 $line = $this->formatter->format($event); 63 64 switch ($event['priority']) { 65 case Logger::EMERG: 66 case Logger::ALERT: 67 case Logger::CRIT: 68 case Logger::ERR: 69 $this->chromephp->error($line); 70 break; 71 case Logger::WARN: 72 $this->chromephp->warn($line); 73 break; 74 case Logger::NOTICE: 75 case Logger::INFO: 76 $this->chromephp->info($line); 77 break; 78 case Logger::DEBUG: 79 $this->chromephp->trace($line); 80 break; 81 default: 82 $this->chromephp->log($line); 83 break; 84 } 85 } 86 87 /** 88 * Gets the ChromePhpInterface instance that is used for logging. 89 * 90 * @return ChromePhpInterface 91 */ 92 public function getChromePhp() 93 { 94 // Remember: class names in strings are absolute; thus the class_exists 95 // here references the canonical name for the ChromePhp class 96 if (! $this->chromephp instanceof ChromePhpInterface 97 && class_exists('ChromePhp') 98 ) { 99 $this->setChromePhp(new ChromePhpBridge()); 100 } 101 return $this->chromephp; 102 } 103 104 /** 105 * Sets the ChromePhpInterface instance that is used for logging. 106 * 107 * @param ChromePhpInterface $instance The instance to set. 108 * @return ChromePhp 109 */ 110 public function setChromePhp(ChromePhpInterface $instance) 111 { 112 $this->chromephp = $instance; 113 return $this; 114 } 115} 116