Passed
Pull Request — master (#988)
by
unknown
17:05
created

TStdOutLogRoute::levelColor()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 21
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 23
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
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...
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]));
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

61
			$writer->write('[' . static::/** @scrutinizer ignore-call */ getLevelName($log[TLogger::LOG_LEVEL]) . ']', $this->levelColor($log[TLogger::LOG_LEVEL]));
Loading history...
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