1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TStdOutLogRoute class file |
4
|
|
|
* |
5
|
|
|
* @author Brad Anderson <[email protected]> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Prado\Util; |
11
|
|
|
|
12
|
|
|
use Prado\Exceptions\TConfigurationException; |
13
|
|
|
use Prado\Exceptions\TLogException; |
14
|
|
|
use Prado\IO\TStdOutWriter; |
15
|
|
|
use Prado\Prado; |
16
|
|
|
use Prado\TPropertyValue; |
17
|
|
|
use Prado\Shell\Actions\TWebServerAction; |
18
|
|
|
use Prado\Shell\TShellWriter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* TStdOutLogRoute class. |
22
|
|
|
* |
23
|
|
|
* This sends the log to STDOUT and will corrupt web server log files. This is useful |
24
|
|
|
* in presenting PRADO logs in the PHP (Test and Development) Web Server output. |
25
|
|
|
* |
26
|
|
|
* The TStdOutLogRoute can be turned off for all but the built-in Test web server |
27
|
|
|
* with the configuration property {@see \Prado\Util\TStdOutLogRoute::getOnlyDevServer} |
28
|
|
|
* set to true. |
29
|
|
|
* |
30
|
|
|
* @author Brad Anderson <[email protected]> |
31
|
|
|
* @since 4.2.3 |
32
|
|
|
*/ |
33
|
|
|
class TStdOutLogRoute extends TLogRoute |
34
|
|
|
{ |
35
|
|
|
/** @var bool Enable this log route only when running the built-in PHP web server, default false */ |
36
|
|
|
private bool $_onlyDevServer = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This returns if the route is enabled. When OnlyDevServer is true, then this route |
40
|
|
|
* will only enable when running a page in the PHP Web Server. |
41
|
|
|
* @return bool Is the route enabled, default true. |
42
|
|
|
*/ |
43
|
|
|
public function getEnabled(): bool |
44
|
|
|
{ |
45
|
|
|
$enabled = parent::getEnabled(); |
46
|
|
|
|
47
|
|
|
if ($this->getOnlyDevServer()) { |
48
|
|
|
$enabled &= (int) getenv(TWebServerAction::DEV_WEBSERVER_ENV); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $enabled; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $logs list of log messages |
56
|
|
|
* @param bool $final is the final flush |
57
|
|
|
* @param array $meta the meta data for the logs. |
58
|
|
|
* @throws TLogException When failing to write to syslog. |
59
|
|
|
*/ |
60
|
|
|
protected function processLogs(array $logs, bool $final, array $meta) |
61
|
|
|
{ |
62
|
|
|
$writer = new TShellWriter(new TStdOutWriter()); |
63
|
|
|
|
64
|
|
|
foreach ($logs as $log) { |
65
|
|
|
$writer->write('[' . static::getLevelName($log[TLogger::LOG_LEVEL]) . ']', $this->levelColor($log[TLogger::LOG_LEVEL])); |
|
|
|
|
66
|
|
|
$writer->writeLine($this->formatLogMessage($log)); |
67
|
|
|
} |
68
|
|
|
$writer->flush(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Translates a PRADO log level attribute into one understood by syslog |
73
|
|
|
* @param int $level prado log level |
74
|
|
|
* @return int syslog priority |
75
|
|
|
*/ |
76
|
|
|
protected static function levelColor($level) |
77
|
|
|
{ |
78
|
|
|
switch ($level) { |
79
|
|
|
case TLogger::PROFILE: |
80
|
|
|
case TLogger::PROFILE_BEGIN: |
81
|
|
|
case TLogger::PROFILE_END: |
82
|
|
|
return TShellWriter::BG_LIGHT_GREEN; |
83
|
|
|
case TLogger::DEBUG: |
84
|
|
|
return TShellWriter::BG_GREEN; |
85
|
|
|
case TLogger::INFO: |
86
|
|
|
return null; |
87
|
|
|
case TLogger::NOTICE: |
88
|
|
|
return TShellWriter::BG_BLUE; |
89
|
|
|
case TLogger::WARNING: |
90
|
|
|
return TShellWriter::BG_CYAN; |
91
|
|
|
case TLogger::ERROR: |
92
|
|
|
return TShellWriter::BG_LIGHT_MAGENTA; |
93
|
|
|
case TLogger::ALERT: |
94
|
|
|
return TShellWriter::BG_MAGENTA; |
95
|
|
|
case TLogger::FATAL: |
96
|
|
|
return TShellWriter::BG_RED; |
97
|
|
|
default: |
98
|
|
|
return TShellWriter::BG_GREEN; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function formatLogMessage(array $log): string |
106
|
|
|
{ |
107
|
|
|
if (!is_string($log[TLogger::LOG_MESSAGE])) { |
108
|
|
|
if ($log[TLogger::LOG_MESSAGE] instanceof \Exception || $log[TLogger::LOG_MESSAGE] instanceof \Throwable) { |
109
|
|
|
$log[TLogger::LOG_MESSAGE] = (string) $log[TLogger::LOG_MESSAGE]; |
110
|
|
|
} else { |
111
|
|
|
$log[TLogger::LOG_MESSAGE] = \Prado\Util\TVarDumper::dump($log[TLogger::LOG_MESSAGE]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return '[' . $log[TLogger::LOG_CATEGORY] . '] ' . $log[TLogger::LOG_MESSAGE]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return bool If the Route is only enabled when operating in the PHP Test Web |
120
|
|
|
* Server, default false. |
121
|
|
|
*/ |
122
|
|
|
public function getOnlyDevServer(): bool |
123
|
|
|
{ |
124
|
|
|
return $this->_onlyDevServer; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* |
129
|
|
|
* @param bool|string $value Only enable the route when running in the PHP Test Web Server. |
130
|
|
|
* @return static The current object. |
131
|
|
|
*/ |
132
|
|
|
public function setOnlyDevServer($value): static |
133
|
|
|
{ |
134
|
|
|
$this->_onlyDevServer = TPropertyValue::ensureBoolean($value); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|