Issues (1474)

framework/Util/TStdOutLogRoute.php (2 issues)

1
<?php
2
3
/**
4
 * TStdOutLogRoute class file
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Util;
12
13
use Prado\Exceptions\TConfigurationException;
14
use Prado\Exceptions\TLogException;
15
use Prado\IO\TStdOutWriter;
16
use Prado\Prado;
17
use Prado\TPropertyValue;
18
use Prado\Shell\Actions\TWebServerAction;
19
use Prado\Shell\TShellWriter;
20
21
/**
22
 * TStdOutLogRoute class.
23
 *
24
 * This sends the log to STDOUT and will corrupt web server log files.  This is useful
25
 * in presenting PRADO logs in the PHP (Test and Development) Web Server output.
26
 *
27
 * The TStdOutLogRoute can be turned off for all but the built-in Test web server
28
 * with the configuration property {@see \Prado\Util\TStdOutLogRoute::getOnlyDevServer}
29
 * set to true.
30
 *
31
 * @author Brad Anderson <[email protected]>
32
 * @since 4.3.0
33
 */
34
class TStdOutLogRoute extends TLogRoute
35
{
36
	/** @var bool Enable this log route only when running the built-in PHP web server, default false */
37
	private bool $_onlyDevServer = false;
38
39
	/**
40
	 * This returns if the route is enabled.  When OnlyDevServer is true, then this route
41
	 * will only enable when running a page in the PHP Web Server.
42
	 * @return bool Is the route enabled, default true.
43
	 */
44
	public function getEnabled(): bool
45
	{
46
		$enabled = parent::getEnabled();
47
48
		if ($this->getOnlyDevServer()) {
49
			$enabled &= (int) getenv(TWebServerAction::DEV_WEBSERVER_ENV);
50
		}
51
52
		return $enabled;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $enabled could return the type integer which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
53
	}
54
55
	/**
56
	 * @param array $logs list of log messages
57
	 * @param bool $final is the final flush
58
	 * @param array $meta the meta data for the logs.
59
	 * @throws TLogException When failing to write to syslog.
60
	 */
61
	protected function processLogs(array $logs, bool $final, array $meta)
62
	{
63
		$writer = new TShellWriter(new TStdOutWriter());
64
65
		foreach ($logs as $log) {
66
			$writer->write('[' . static::getLevelName($log[TLogger::LOG_LEVEL]) . ']', $this->levelColor($log[TLogger::LOG_LEVEL]));
0 ignored issues
show
Bug Best Practice introduced by
The method Prado\Util\TLogRoute::getLevelName() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
			$writer->write('[' . static::/** @scrutinizer ignore-call */ getLevelName($log[TLogger::LOG_LEVEL]) . ']', $this->levelColor($log[TLogger::LOG_LEVEL]));
Loading history...
67
			$writer->writeLine($this->formatLogMessage($log));
68
		}
69
		$writer->flush();
70
	}
71
72
	/**
73
	 * Translates a PRADO log level attribute into one understood by syslog
74
	 * @param int $level prado log level
75
	 * @return int syslog priority
76
	 */
77
	protected static function levelColor($level)
78
	{
79
		switch ($level) {
80
			case TLogger::PROFILE:
81
			case TLogger::PROFILE_BEGIN:
82
			case TLogger::PROFILE_END:
83
				return TShellWriter::BG_LIGHT_GREEN;
84
			case TLogger::DEBUG:
85
				return TShellWriter::BG_GREEN;
86
			case TLogger::INFO:
87
				return null;
88
			case TLogger::NOTICE:
89
				return TShellWriter::BG_BLUE;
90
			case TLogger::WARNING:
91
				return TShellWriter::BG_CYAN;
92
			case TLogger::ERROR:
93
				return TShellWriter::BG_LIGHT_MAGENTA;
94
			case TLogger::ALERT:
95
				return TShellWriter::BG_MAGENTA;
96
			case TLogger::FATAL:
97
				return TShellWriter::BG_RED;
98
			default:
99
				return TShellWriter::BG_GREEN;
100
		}
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function formatLogMessage(array $log): string
107
	{
108
		if (!is_string($log[TLogger::LOG_MESSAGE])) {
109
			if ($log[TLogger::LOG_MESSAGE] instanceof \Exception || $log[TLogger::LOG_MESSAGE] instanceof \Throwable) {
110
				$log[TLogger::LOG_MESSAGE] = (string) $log[TLogger::LOG_MESSAGE];
111
			} else {
112
				$log[TLogger::LOG_MESSAGE] = \Prado\Util\TVarDumper::dump($log[TLogger::LOG_MESSAGE]);
113
			}
114
		}
115
116
		return '[' . $log[TLogger::LOG_CATEGORY] . '] ' . $log[TLogger::LOG_MESSAGE];
117
	}
118
119
	/**
120
	 * @return bool If the Route is only enabled when operating in the PHP Test Web
121
	 *   Server, default false.
122
	 */
123
	public function getOnlyDevServer(): bool
124
	{
125
		return $this->_onlyDevServer;
126
	}
127
128
	/**
129
	 *
130
	 * @param bool|string $value Only enable the route when running in the PHP Test Web Server.
131
	 * @return static The current object.
132
	 */
133
	public function setOnlyDevServer($value): static
134
	{
135
		$this->_onlyDevServer = TPropertyValue::ensureBoolean($value);
136
137
		return $this;
138
	}
139
}
140