|
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
|
|
|
* @author Brad Anderson <[email protected]> |
|
27
|
|
|
* @since 4.2.3 |
|
28
|
|
|
*/ |
|
29
|
|
|
class TStdOutLogRoute extends TLogRoute |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var bool Enable this log route only when running the built-in PHP web server, default false */ |
|
32
|
|
|
private bool $_onlyDevServer = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* This returns if the route is enabled. When OnlyDevServer is true, then this route |
|
36
|
|
|
* will only enable when running a page in the PHP Web Server. |
|
37
|
|
|
* @return bool Is the route enabled, default true. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getEnabled(): bool |
|
40
|
|
|
{ |
|
41
|
|
|
$enabled = parent::getEnabled(); |
|
42
|
|
|
|
|
43
|
|
|
if ($this->getOnlyDevServer()) { |
|
44
|
|
|
$enabled &= (int) getenv(TWebServerAction::DEV_WEBSERVER_ENV); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $enabled; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $logs list of log messages |
|
52
|
|
|
* @param bool $final is the final flush |
|
53
|
|
|
* @param array $meta the meta data for the logs. |
|
54
|
|
|
* @throws TLogException When failing to write to syslog. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function processLogs(array $logs, bool $final, array $meta) |
|
57
|
|
|
{ |
|
58
|
|
|
$writer = new TShellWriter(new TStdOutWriter()); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($logs as $log) { |
|
61
|
|
|
$writer->write('[' . static::getLevelName($log[TLogger::LOG_LEVEL]) . ']', $this->levelColor($log[TLogger::LOG_LEVEL])); |
|
|
|
|
|
|
62
|
|
|
$writer->writeLine($this->formatLogMessage($log)); |
|
63
|
|
|
} |
|
64
|
|
|
$writer->flush(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Translates a PRADO log level attribute into one understood by syslog |
|
69
|
|
|
* @param int $level prado log level |
|
70
|
|
|
* @return int syslog priority |
|
71
|
|
|
*/ |
|
72
|
|
|
protected static function levelColor($level) |
|
73
|
|
|
{ |
|
74
|
|
|
switch ($level) { |
|
75
|
|
|
case TLogger::PROFILE: |
|
76
|
|
|
case TLogger::PROFILE_BEGIN: |
|
77
|
|
|
case TLogger::PROFILE_END: |
|
78
|
|
|
return TShellWriter::BG_LIGHT_GREEN; |
|
79
|
|
|
case TLogger::DEBUG: |
|
80
|
|
|
return TShellWriter::BG_GREEN; |
|
81
|
|
|
case TLogger::INFO: |
|
82
|
|
|
return null; |
|
83
|
|
|
case TLogger::NOTICE: |
|
84
|
|
|
return TShellWriter::BG_BLUE; |
|
85
|
|
|
case TLogger::WARNING: |
|
86
|
|
|
return TShellWriter::BG_CYAN; |
|
87
|
|
|
case TLogger::ERROR: |
|
88
|
|
|
return TShellWriter::BG_LIGHT_MAGENTA; |
|
89
|
|
|
case TLogger::ALERT: |
|
90
|
|
|
return TShellWriter::BG_MAGENTA; |
|
91
|
|
|
case TLogger::FATAL: |
|
92
|
|
|
return TShellWriter::BG_RED; |
|
93
|
|
|
default: |
|
94
|
|
|
return TShellWriter::BG_GREEN; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function formatLogMessage(array $log): string |
|
102
|
|
|
{ |
|
103
|
|
|
if (!is_string($log[TLogger::LOG_MESSAGE])) { |
|
104
|
|
|
if ($log[TLogger::LOG_MESSAGE] instanceof \Exception || $log[TLogger::LOG_MESSAGE] instanceof \Throwable) { |
|
105
|
|
|
$log[TLogger::LOG_MESSAGE] = (string) $log[TLogger::LOG_MESSAGE]; |
|
106
|
|
|
} else { |
|
107
|
|
|
$log[TLogger::LOG_MESSAGE] = \Prado\Util\TVarDumper::dump($log[TLogger::LOG_MESSAGE]); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return '[' . $log[TLogger::LOG_CATEGORY] . '] ' . $log[TLogger::LOG_MESSAGE]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return bool If the Route is only enabled when operating in the PHP Test Web |
|
116
|
|
|
* Server, default false. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getOnlyDevServer(): bool |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->_onlyDevServer; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* |
|
125
|
|
|
* @param bool|string $value Only enable the route when running in the PHP Test Web Server. |
|
126
|
|
|
* @return static The current object. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setOnlyDevServer($value): static |
|
129
|
|
|
{ |
|
130
|
|
|
$this->_onlyDevServer = TPropertyValue::ensureBoolean($value); |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|