Completed
Push — master ( 5dae0c...c63b8d )
by Mark
04:53 queued 51s
created

ConsoleLog::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * CakePHP(tm) :  Rapid Development Framework (https://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11
 * @link          https://cakefoundation.org CakePHP(tm) Project
12
 * @since         2.2.0
13
 * @license       https://opensource.org/licenses/mit-license.php MIT License
14
 */
15
namespace Cake\Log\Engine;
16
17
use Cake\Console\ConsoleOutput;
18
use InvalidArgumentException;
19
20
/**
21
 * Console logging. Writes logs to console output.
22
 */
23
class ConsoleLog extends BaseLog
24
{
25
26
    /**
27
     * Default config for this class
28
     *
29
     * @var array
30
     */
31
    protected $_defaultConfig = [
32
        'stream' => 'php://stderr',
33
        'levels' => null,
34
        'scopes' => [],
35
        'outputAs' => null,
36
    ];
37
38
    /**
39
     * Output stream
40
     *
41
     * @var \Cake\Console\ConsoleOutput
42
     */
43
    protected $_output;
44
45
    /**
46
     * Constructs a new Console Logger.
47
     *
48
     * Config
49
     *
50
     * - `levels` string or array, levels the engine is interested in
51
     * - `scopes` string or array, scopes the engine is interested in
52
     * - `stream` the path to save logs on.
53
     * - `outputAs` integer or ConsoleOutput::[RAW|PLAIN|COLOR]
54
     *
55
     * @param array $config Options for the FileLog, see above.
56
     * @throws \InvalidArgumentException
57
     */
58
    public function __construct(array $config = [])
59
    {
60
        parent::__construct($config);
61
62
        $config = $this->_config;
63
        if ($config['stream'] instanceof ConsoleOutput) {
64
            $this->_output = $config['stream'];
65
        } elseif (is_string($config['stream'])) {
66
            $this->_output = new ConsoleOutput($config['stream']);
67
        } else {
68
            throw new InvalidArgumentException('`stream` not a ConsoleOutput nor string');
69
        }
70
71
        if (isset($config['outputAs'])) {
72
            $this->_output->setOutputAs($config['outputAs']);
0 ignored issues
show
Documentation introduced by
$config['outputAs'] is of type object<Cake\Console\ConsoleOutput>|string, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        }
74
    }
75
76
    /**
77
     * Implements writing to console.
78
     *
79
     * @param string $level The severity level of log you are making.
80
     * @param string $message The message you want to log.
81
     * @param array $context Additional information about the logged message
82
     * @return bool success of write.
83
     */
84
    public function log($level, $message, array $context = [])
85
    {
86
        $message = $this->_format($message, $context);
87
        $output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message;
88
89
        return (bool)$this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level));
90
    }
91
}
92