|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Monolog package. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MonologBundle\Handler; |
| 13 | + |
| 14 | +use Monolog\Handler\AbstractProcessingHandler; |
| 15 | +use Monolog\Logger; |
| 16 | +use Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | +use Symfony\Component\Console\Output\StreamOutput; |
| 19 | + |
| 20 | +/** |
| 21 | + * Writes to the console output depending on its verbosity setting. |
| 22 | + * |
| 23 | + * It is disabled by default and gets activated when setOutput is called. |
| 24 | + * |
| 25 | + * @author Tobias Schultze <http://tobion.de> |
| 26 | + */ |
| 27 | +class ConsoleHandler extends AbstractProcessingHandler |
| 28 | +{ |
| 29 | + /** |
| 30 | + * The stream to use when the console OutputInterface is not a StreamOutput. |
| 31 | + */ |
| 32 | + const FALLBACK_STREAM = 'php://output'; |
| 33 | + |
| 34 | + /** |
| 35 | + * The error stream to use when the console OutputInterface is not a StreamOutput. |
| 36 | + */ |
| 37 | + const FALLBACK_ERROR_STREAM = 'php://stderr'; |
| 38 | + |
| 39 | + private $enabled = false; |
| 40 | + private $stream; |
| 41 | + private $streamCreated = false; |
| 42 | + private $errorStream; |
| 43 | + private $errorStreamCreated = false; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor. |
| 47 | + * |
| 48 | + * The minimum logging level it is set based on the output verbosity in setOutput. |
| 49 | + * |
| 50 | + * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
| 51 | + */ |
| 52 | + public function __construct($bubble = true) |
| 53 | + { |
| 54 | + parent::__construct(Logger::DEBUG, $bubble); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * {@inheritdoc} |
| 59 | + */ |
| 60 | + public function isHandling(array $record) |
| 61 | + { |
| 62 | + return $this->enabled && parent::isHandling($record); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function handle(array $record) |
| 69 | + { |
| 70 | + if (!$this->enabled) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + return parent::handle($record); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Sets the console output to use for printing logs. |
| 79 | + * |
| 80 | + * It enabled the writing unless the output verbosity is set to quiet. |
| 81 | + * |
| 82 | + * @param OutputInterface $output The console output to use |
| 83 | + */ |
| 84 | + public function setOutput(OutputInterface $output) |
| 85 | + { |
| 86 | + // close streams that might have been created before |
| 87 | + $this->close(); |
| 88 | + |
| 89 | + if (OutputInterface::VERBOSITY_QUIET === $output->getVerbosity()) { |
| 90 | + return; // the handler remains disabled |
| 91 | + } |
| 92 | + |
| 93 | + if ($output instanceof StreamOutput) { |
| 94 | + $this->stream = $output->getStream(); |
| 95 | + } |
| 96 | + |
| 97 | + if ($output instanceof ConsoleOutputInterface && $output->getErrorOutput() instanceof StreamOutput) { |
| 98 | + $this->errorStream = $output->getErrorOutput()->getStream(); |
| 99 | + } |
| 100 | + |
| 101 | + switch ($output->getVerbosity()) { |
| 102 | + case OutputInterface::VERBOSITY_NORMAL: |
| 103 | + $this->setLevel(Logger::WARNING); |
| 104 | + break; |
| 105 | + case OutputInterface::VERBOSITY_VERBOSE: |
| 106 | + $this->setLevel(Logger::NOTICE); |
| 107 | + break; |
| 108 | + case OutputInterface::VERBOSITY_VERY_VERBOSE: |
| 109 | + $this->setLevel(Logger::INFO); |
| 110 | + break; |
| 111 | + default: |
| 112 | + $this->setLevel(Logger::DEBUG); |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + $this->enabled = true; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Disables the output and closes newly created streams. |
| 121 | + * |
| 122 | + * It does not close streams coming from StreamOutput because they belong to the console. |
| 123 | + */ |
| 124 | + public function close() |
| 125 | + { |
| 126 | + if ($this->streamCreated && is_resource($this->stream)) { |
| 127 | + fclose($this->stream); |
| 128 | + } |
| 129 | + $this->stream = null; |
| 130 | + $this->streamCreated = false; |
| 131 | + |
| 132 | + if ($this->errorStreamCreated && is_resource($this->errorStream)) { |
| 133 | + fclose($this->errorStream); |
| 134 | + } |
| 135 | + $this->errorStream = null; |
| 136 | + $this->errorStreamCreated = false; |
| 137 | + |
| 138 | + $this->enabled = false; |
| 139 | + |
| 140 | + parent::close(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * {@inheritdoc} |
| 145 | + */ |
| 146 | + protected function write(array $record) |
| 147 | + { |
| 148 | + if (null === $this->stream) { |
| 149 | + $errorMessage = null; |
| 150 | + set_error_handler(function ($code, $msg) use (&$errorMessage) { |
| 151 | + $errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg); |
| 152 | + }); |
| 153 | + $this->stream = fopen(self::FALLBACK_STREAM, 'a'); |
| 154 | + restore_error_handler(); |
| 155 | + if (!is_resource($this->stream)) { |
| 156 | + $this->stream = null; |
| 157 | + $this->streamCreated = false; |
| 158 | + throw new \UnexpectedValueException(sprintf('The stream "%s" could not be opened: '.$errorMessage, self::FALLBACK_STREAM)); |
| 159 | + } |
| 160 | + $this->streamCreated = true; |
| 161 | + } |
| 162 | + |
| 163 | + if (null === $this->errorStream) { |
| 164 | + $errorMessage = null; |
| 165 | + set_error_handler(function ($code, $msg) use (&$errorMessage) { |
| 166 | + $errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg); |
| 167 | + }); |
| 168 | + $this->errorStream = fopen(self::FALLBACK_ERROR_STREAM, 'a'); |
| 169 | + restore_error_handler(); |
| 170 | + if (!is_resource($this->errorStream)) { |
| 171 | + $this->errorStream = null; |
| 172 | + $this->errorStreamCreated = false; |
| 173 | + throw new \UnexpectedValueException(sprintf('The stream "%s" could not be opened: '.$errorMessage, self::FALLBACK_STREAM)); |
| 174 | + } |
| 175 | + $this->errorStreamCreated = true; |
| 176 | + } |
| 177 | + |
| 178 | + if ($record['level'] >= Logger::ERROR) { |
| 179 | + fwrite($this->errorStream, (string) $record['formatted']); |
| 180 | + } else { |
| 181 | + fwrite($this->stream, (string) $record['formatted']); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments