Skip to content

Commit c52ac9f

Browse files
authored
Fix and add checkings to Target and Logger (#46)
* Replace checks with shorter ones in Logger::log() * Change methods visibility * Add use of DEFAULT_CATEGORY constant * Add check server variable REQUEST_TIME_FLOAT * Add message structure check to Target * Remove Logger::getElapsedTime() method * Change protected static methods on normal
1 parent a8cd64f commit c52ac9f

5 files changed

Lines changed: 188 additions & 133 deletions

File tree

src/Logger.php

Lines changed: 65 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ public function __construct(array $targets = [])
106106
});
107107
}
108108

109+
/**
110+
* Returns the text display of the specified level.
111+
* @param mixed $level the message level, e.g. {@see LogLevel::ERROR}, {@see LogLevel::WARNING}.
112+
* @return string the text display of the level
113+
*/
114+
public static function getLevelName($level): string
115+
{
116+
if (is_string($level)) {
117+
return $level;
118+
}
119+
return 'unknown';
120+
}
121+
109122
/**
110123
* @return Target[] the log targets. Each array element represents a single {@see \Yiisoft\Log\Target} instance.
111124
*/
@@ -152,47 +165,20 @@ public function addTarget(Target $target, string $name = null): void
152165
}
153166
}
154167

155-
/**
156-
* Prepares message for logging.
157-
* @param mixed $message
158-
* @return string
159-
*/
160-
public static function prepareMessage($message): string
161-
{
162-
if (method_exists($message, '__toString')) {
163-
return (string)$message;
164-
}
165-
166-
if (is_scalar($message)) {
167-
return (string)$message;
168-
}
169-
170-
return VarDumper::create($message)->export();
171-
}
172-
173168
public function log($level, $message, array $context = []): void
174169
{
175170
if (($message instanceof Throwable) && !isset($context['exception'])) {
176171
// exceptions are string-convertible, thus should be passed as it is to the logger
177172
// if exception instance is given to produce a stack trace, it MUST be in a key named "exception".
178173
$context['exception'] = $message;
179174
}
180-
$message = static::prepareMessage($message);
181175

182-
if (!isset($context['time'])) {
183-
$context['time'] = microtime(true);
184-
}
185-
if (!isset($context['trace'])) {
186-
$context['trace'] = $this->collectTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
187-
}
176+
$message = $this->prepareMessage($message);
188177

189-
if (!isset($context['memory'])) {
190-
$context['memory'] = memory_get_usage();
191-
}
192-
193-
if (!isset($context['category'])) {
194-
$context['category'] = 'application';
195-
}
178+
$context['time'] ??= microtime(true);
179+
$context['trace'] ??= $this->collectTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
180+
$context['memory'] ??= memory_get_usage();
181+
$context['category'] ??= Target::DEFAULT_CATEGORY;
196182

197183
$message = $this->parseMessage($message, $context);
198184

@@ -217,6 +203,34 @@ public function flush(bool $final = false): void
217203
$this->dispatch($messages, $final);
218204
}
219205

206+
public function getFlushInterval(): int
207+
{
208+
return $this->flushInterval;
209+
}
210+
211+
public function setFlushInterval(int $flushInterval): self
212+
{
213+
$this->flushInterval = $flushInterval;
214+
return $this;
215+
}
216+
217+
public function getTraceLevel(): int
218+
{
219+
return $this->traceLevel;
220+
}
221+
222+
public function setTraceLevel(int $traceLevel): self
223+
{
224+
$this->traceLevel = $traceLevel;
225+
return $this;
226+
}
227+
228+
public function setExcludedTracePaths(array $excludedTracePaths): self
229+
{
230+
$this->excludedTracePaths = $excludedTracePaths;
231+
return $this;
232+
}
233+
220234
/**
221235
* Dispatches the logged messages to {@see Logger::$targets}.
222236
* @param array $messages the logged messages
@@ -247,6 +261,24 @@ protected function dispatch(array $messages, bool $final): void
247261
}
248262
}
249263

264+
/**
265+
* Prepares message for logging.
266+
* @param mixed $message
267+
* @return string
268+
*/
269+
protected function prepareMessage($message): string
270+
{
271+
if (method_exists($message, '__toString')) {
272+
return (string) $message;
273+
}
274+
275+
if (is_scalar($message)) {
276+
return (string) $message;
277+
}
278+
279+
return VarDumper::create($message)->export();
280+
}
281+
250282
/**
251283
* Parses log message resolving placeholders in the form: '{foo}', where foo
252284
* will be replaced by the context data in key "foo".
@@ -259,64 +291,12 @@ protected function parseMessage(string $message, array $context): string
259291
return preg_replace_callback('/{([\w.]+)}/', static function (array $matches) use ($context) {
260292
$placeholderName = $matches[1];
261293
if (isset($context[$placeholderName])) {
262-
return (string)$context[$placeholderName];
294+
return (string) $context[$placeholderName];
263295
}
264296
return $matches[0];
265297
}, $message);
266298
}
267299

268-
/**
269-
* Returns the total elapsed time since the start of the current request.
270-
* This method calculates the difference between now and the start of the
271-
* request ($_SERVER['REQUEST_TIME_FLOAT']).
272-
* @return float the total elapsed time in seconds for current request.
273-
*/
274-
public function getElapsedTime(): float
275-
{
276-
return \microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
277-
}
278-
279-
/**
280-
* Returns the text display of the specified level.
281-
* @param mixed $level the message level, e.g. {@see LogLevel::ERROR}, {@see LogLevel::WARNING}.
282-
* @return string the text display of the level
283-
*/
284-
public static function getLevelName($level): string
285-
{
286-
if (is_string($level)) {
287-
return $level;
288-
}
289-
return 'unknown';
290-
}
291-
292-
public function getFlushInterval(): int
293-
{
294-
return $this->flushInterval;
295-
}
296-
297-
public function setFlushInterval(int $flushInterval): self
298-
{
299-
$this->flushInterval = $flushInterval;
300-
return $this;
301-
}
302-
303-
public function getTraceLevel(): int
304-
{
305-
return $this->traceLevel;
306-
}
307-
308-
public function setTraceLevel(int $traceLevel): self
309-
{
310-
$this->traceLevel = $traceLevel;
311-
return $this;
312-
}
313-
314-
public function setExcludedTracePaths(array $excludedTracePaths): self
315-
{
316-
$this->excludedTracePaths = $excludedTracePaths;
317-
return $this;
318-
}
319-
320300
private function collectTrace(array $backtrace): array
321301
{
322302
$traces = [];

0 commit comments

Comments
 (0)