-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamTarget.php
74 lines (65 loc) · 1.86 KB
/
StreamTarget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace consultnn\streamTarget;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\log\Logger;
use yii\log\Target;
/**
* Class StreamTarget
* Implements log target for php stream wrappers (http://php.net/manual/ru/wrappers.php)
* @package project\vendor\consultnn\streamTarget
*/
class StreamTarget extends Target
{
public $stream = 'php://stdout';
private $_resource ;
public function init()
{
if (empty($this->stream)) {
throw new InvalidConfigException("No stream configured.");
}
if (($this->_resource = @fopen($this->stream, 'w')) === false) {
throw new InvalidConfigException("Unable to append to '{$this->stream}'");
}
}
/**
* @inheritdoc
*/
public function export()
{
foreach ($this->messages as $message) {
fwrite($this->_resource, $this->formatMessage($message).PHP_EOL);
}
}
/**
* @inheritdoc
*/
public function formatMessage($message)
{
list($text, $level, $category, $timestamp) = $message;
$level = Logger::getLevelName($level);
if (!is_string($text)) {
// exceptions may not be serializable if in the call stack somewhere is a Closure
if ($text instanceof \Exception) {
$text = (string) $text;
} else {
$text = Json::encode($text);
}
}
$prefix = $this->getMessagePrefix($message);
return "{$prefix}[$level][$category] $text";
}
/**
* @inheritdoc
*/
protected function getContextMessage()
{
$context = [];
foreach ($this->logVars as $name) {
if (!empty($GLOBALS[$name])) {
$context[] = "\${$name} = " . Json::encode($GLOBALS[$name]);
}
}
return implode("\t", $context);
}
}