ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
StreamHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
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 Monolog\Handler;
13 
14 use Monolog\Logger;
15 
24 {
25  protected $stream;
26  protected $url;
27  private $errorMessage;
28  protected $filePermission;
29  protected $useLocking;
30  private $dirCreated;
31 
42  public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
43  {
44  parent::__construct($level, $bubble);
45  if (is_resource($stream)) {
46  $this->stream = $stream;
47  } elseif (is_string($stream)) {
48  $this->url = $stream;
49  } else {
50  throw new \InvalidArgumentException('A stream must either be a resource or a string.');
51  }
52 
53  $this->filePermission = $filePermission;
54  $this->useLocking = $useLocking;
55  }
56 
60  public function close()
61  {
62  if ($this->url && is_resource($this->stream)) {
63  fclose($this->stream);
64  }
65  $this->stream = null;
66  }
67 
73  public function getStream()
74  {
75  return $this->stream;
76  }
77 
83  public function getUrl()
84  {
85  return $this->url;
86  }
87 
91  protected function write(array $record)
92  {
93  if (!is_resource($this->stream)) {
94  if (null === $this->url || '' === $this->url) {
95  throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
96  }
97  $this->createDir();
98  $this->errorMessage = null;
99  set_error_handler(array($this, 'customErrorHandler'));
100  $this->stream = fopen($this->url, 'a');
101  if ($this->filePermission !== null) {
102  @chmod($this->url, $this->filePermission);
103  }
104  restore_error_handler();
105  if (!is_resource($this->stream)) {
106  $this->stream = null;
107  throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
108  }
109  }
110 
111  if ($this->useLocking) {
112  // ignoring errors here, there's not much we can do about them
113  flock($this->stream, LOCK_EX);
114  }
115 
116  fwrite($this->stream, (string) $record['formatted']);
117 
118  if ($this->useLocking) {
119  flock($this->stream, LOCK_UN);
120  }
121  }
122 
123  private function customErrorHandler($code, $msg)
124  {
125  $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
126  }
127 
133  private function getDirFromStream($stream)
134  {
135  $pos = strpos($stream, '://');
136  if ($pos === false) {
137  return dirname($stream);
138  }
139 
140  if ('file://' === substr($stream, 0, 7)) {
141  return dirname(substr($stream, 7));
142  }
143 
144  return;
145  }
146 
147  private function createDir()
148  {
149  // Do not try to create dir if it has already been tried.
150  if ($this->dirCreated) {
151  return;
152  }
153 
154  $dir = $this->getDirFromStream($this->url);
155  if (null !== $dir && !is_dir($dir)) {
156  $this->errorMessage = null;
157  set_error_handler(array($this, 'customErrorHandler'));
158  $status = mkdir($dir, 0777, true);
159  restore_error_handler();
160  if (false === $status) {
161  throw new \UnexpectedValueException(sprintf('There is no existing directory at "%s" and its not buildable: '.$this->errorMessage, $dir));
162  }
163  }
164  $this->dirCreated = true;
165  }
166 }
const DEBUG
Detailed debug information.
Definition: Logger.php:32
$code
Definition: example_050.php:99
Base Handler class providing the Handler structure.
getStream()
Return the currently active stream if it is open.
__construct($stream, $level=Logger::DEBUG, $bubble=true, $filePermission=null, $useLocking=false)
Create styles array
The data for the language used.
Stores to any stream resource.
getUrl()
Return the stream URL if it was configured with a URL and not an active resource. ...