PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 2.0.4
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v2.0.4
4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 All 340 releases
profile-builder / assets / lib / Mustache / Logger / StreamLogger.php

StreamLogger.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 2.0.4, at assets/lib/Mustache/Logger/StreamLogger.php

194 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of Mustache.php.
5 *
6 * (c) 2012 Justin Hileman
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 /**
13 * A Mustache Stream Logger.
14 *
15 * The Stream Logger wraps a file resource instance (such as a stream) or a
16 * stream URL. All log messages over the threshold level will be appended to
17 * this stream.
18 *
19 * Hint: Try `php://stderr` for your stream URL.
20 */
21 class Mustache_Logger_StreamLogger extends Mustache_Logger_AbstractLogger
22 {
23 protected static $levels = array(
24 self::DEBUG => 100,
25 self::INFO => 200,
26 self::NOTICE => 250,
27 self::WARNING => 300,
28 self::ERROR => 400,
29 self::CRITICAL => 500,
30 self::ALERT => 550,
31 self::EMERGENCY => 600,
32 );
33
34 protected $stream = null;
35 protected $url = null;
36
37 /**
38 * @throws InvalidArgumentException if the logging level is unknown.
39 *
40 * @param string $stream Resource instance or URL
41 * @param integer $level The minimum logging level at which this handler will be triggered
42 */
43 public function __construct($stream, $level = Mustache_Logger::ERROR)
44 {
45 $this->setLevel($level);
46
47 if (is_resource($stream)) {
48 $this->stream = $stream;
49 } else {
50 $this->url = $stream;
51 }
52 }
53
54 /**
55 * Close stream resources.
56 */
57 public function __destruct()
58 {
59 if (is_resource($this->stream)) {
60 fclose($this->stream);
61 }
62 }
63
64 /**
65 * Set the minimum logging level.
66 *
67 * @throws Mustache_Exception_InvalidArgumentException if the logging level is unknown.
68 *
69 * @param integer $level The minimum logging level which will be written
70 */
71 public function setLevel($level)
72 {
73 if (!array_key_exists($level, self::$levels)) {
74 throw new Mustache_Exception_InvalidArgumentException(sprintf('Unexpected logging level: %s', $level));
75 }
76
77 $this->level = $level;
78 }
79
80 /**
81 * Get the current minimum logging level.
82 *
83 * @return integer
84 */
85 public function getLevel()
86 {
87 return $this->level;
88 }
89
90 /**
91 * Logs with an arbitrary level.
92 *
93 * @throws Mustache_Exception_InvalidArgumentException if the logging level is unknown.
94 *
95 * @param mixed $level
96 * @param string $message
97 * @param array $context
98 */
99 public function log($level, $message, array $context = array())
100 {
101 if (!array_key_exists($level, self::$levels)) {
102 throw new Mustache_Exception_InvalidArgumentException(sprintf('Unexpected logging level: %s', $level));
103 }
104
105 if (self::$levels[$level] >= self::$levels[$this->level]) {
106 $this->writeLog($level, $message, $context);
107 }
108 }
109
110 /**
111 * Write a record to the log.
112 *
113 * @throws Mustache_Exception_LogicException If neither a stream resource nor url is present.
114 * @throws Mustache_Exception_RuntimeException If the stream url cannot be opened.
115 *
116 * @param integer $level The logging level
117 * @param string $message The log message
118 * @param array $context The log context
119 */
120 protected function writeLog($level, $message, array $context = array())
121 {
122 if (!is_resource($this->stream)) {
123 if (!isset($this->url)) {
124 throw new Mustache_Exception_LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
125 }
126
127 $this->stream = fopen($this->url, 'a');
128 if (!is_resource($this->stream)) {
129 // @codeCoverageIgnoreStart
130 throw new Mustache_Exception_RuntimeException(sprintf('The stream or file "%s" could not be opened.', $this->url));
131 // @codeCoverageIgnoreEnd
132 }
133 }
134
135 fwrite($this->stream, self::formatLine($level, $message, $context));
136 }
137
138 /**
139 * Gets the name of the logging level.
140 *
141 * @throws InvalidArgumentException if the logging level is unknown.
142 *
143 * @param integer $level
144 *
145 * @return string
146 */
147 protected static function getLevelName($level)
148 {
149 return strtoupper($level);
150 }
151
152 /**
153 * Format a log line for output.
154 *
155 * @param integer $level The logging level
156 * @param string $message The log message
157 * @param array $context The log context
158 *
159 * @return string
160 */
161 protected static function formatLine($level, $message, array $context = array())
162 {
163 return sprintf(
164 "%s: %s\n",
165 self::getLevelName($level),
166 self::interpolateMessage($message, $context)
167 );
168 }
169
170 /**
171 * Interpolate context values into the message placeholders.
172 *
173 * @param string $message
174 * @param array $context
175 *
176 * @return string
177 */
178 protected static function interpolateMessage($message, array $context = array())
179 {
180 if (strpos($message, '{') === false) {
181 return $message;
182 }
183
184 // build a replacement array with braces around the context keys
185 $replace = array();
186 foreach ($context as $key => $val) {
187 $replace['{' . $key . '}'] = $val;
188 }
189
190 // interpolate replacement values into the the message and return
191 return strtr($message, $replace);
192 }
193 }
194