ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
12namespace Monolog\Handler;
13
15
24{
25 protected $stream;
26 protected $url;
28 protected $filePermission;
29 protected $useLocking;
30
41 public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
42 {
43 parent::__construct($level, $bubble);
44 if (is_resource($stream)) {
45 $this->stream = $stream;
46 } elseif (is_string($stream)) {
47 $dir = $this->getDirFromStream($stream);
48 if (null !== $dir && !is_dir($dir)) {
49 $this->errorMessage = null;
50 set_error_handler(array($this, 'customErrorHandler'));
51 $status = mkdir($dir, 0777, true);
52 restore_error_handler();
53 if (false === $status) {
54 throw new \UnexpectedValueException(sprintf('There is no existing directory at "%s" and its not buildable: '.$this->errorMessage, $dir));
55 }
56 }
57 $this->url = $stream;
58 } else {
59 throw new \InvalidArgumentException('A stream must either be a resource or a string.');
60 }
61
62 $this->filePermission = $filePermission;
63 $this->useLocking = $useLocking;
64 }
65
69 public function close()
70 {
71 if (is_resource($this->stream)) {
72 fclose($this->stream);
73 }
74 $this->stream = null;
75 }
76
80 protected function write(array $record)
81 {
82 if (!is_resource($this->stream)) {
83 if (!$this->url) {
84 throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
85 }
86 $this->errorMessage = null;
87 set_error_handler(array($this, 'customErrorHandler'));
88 $this->stream = fopen($this->url, 'a');
89 if ($this->filePermission !== null) {
90 @chmod($this->url, $this->filePermission);
91 }
92 restore_error_handler();
93 if (!is_resource($this->stream)) {
94 $this->stream = null;
95 throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
96 }
97 }
98
99 if ($this->useLocking) {
100 // ignoring errors here, there's not much we can do about them
101 flock($this->stream, LOCK_EX);
102 }
103
104 fwrite($this->stream, (string) $record['formatted']);
105
106 if ($this->useLocking) {
107 flock($this->stream, LOCK_UN);
108 }
109 }
110
111 private function customErrorHandler($code, $msg)
112 {
113 $this->errorMessage = preg_replace('{^(fopen|mkdir)\‍(.*?\‍): }', '', $msg);
114 }
115
121 private function getDirFromStream($stream)
122 {
123 $pos = strpos($stream, '://');
124 if ($pos === false) {
125 return dirname($stream);
126 }
127
128 if ('file://' === substr($stream, 0, 7)) {
129 return dirname(substr($stream, 7));
130 }
131
132 return;
133 }
134}
Base Handler class providing the Handler structure.
Stores to any stream resource.
write(array $record)
{Writes the record down to the log of the implementing handler.void}
close()
{Closes the handler.This will be called automatically when the object is destroyed}
__construct($stream, $level=Logger::DEBUG, $bubble=true, $filePermission=null, $useLocking=false)
Monolog log channel.
Definition: Logger.php:28
const DEBUG
Detailed debug information.
Definition: Logger.php:32
$code
Definition: example_050.php:99