Passed
Push — master ( 6ce998...91641e )
by Fabio
05:04
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
 * 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;
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...
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]));
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

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