ConsoleTarget::getContextMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\log;
12
13
use Psr\Log\LogLevel;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LogLevel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use yii\helpers\Console;
15
use yii\helpers\VarDumper;
16
17
class ConsoleTarget extends \yii\log\Target
18
{
19
    public $exportInterval = 1;
20
21
    public $exportContext = [
22
        LogLevel::EMERGENCY => false,
23
        LogLevel::ERROR     => false,
24
        LogLevel::ALERT     => false,
25
        LogLevel::CRITICAL  => false,
26
        LogLevel::WARNING   => false,
27
    ];
28
29
    public static $styles = [
30
        LogLevel::EMERGENCY => [Console::BOLD, Console::BG_RED],
31
        LogLevel::ERROR     => [Console::FG_RED, Console::BOLD],
32
        LogLevel::ALERT     => [Console::FG_RED],
33
        LogLevel::CRITICAL  => [Console::FG_RED],
34
        LogLevel::WARNING   => [Console::FG_YELLOW],
35
    ];
36
37
    public function export()
38
    {
39
        foreach ($this->messages as $message) {
40
            $this->out($message[0], $message[1]);
41
            $this->outContext($message[0], $message[2]);
42
        }
43
    }
44
45
    private function outContext($level, $context)
46
    {
47
        if ($this->exportContext[$level] ?? false) {
48
            $export = VarDumper::export($context);
49
            Console::stdout($export . "\n");
50
        }
51
    }
52
53
    public function out($level, $message)
54
    {
55
        /* XXX add minimum log severity to show
56
         * if ($level > $this->getLevel()) {
57
            return;
58
        }*/
59
        $style = self::$styles[$level];
60
        if ($style) {
61
            $message = Console::ansiFormat($message, $style);
62
        } else {
63
            return;
64
        }
65
        Console::stdout($message . "\n");
66
    }
67
68
    protected function getContextMessage()
69
    {
70
        return '';
71
    }
72
}
73