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' => 'see constructor'
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: if ((DIRECTORY_SEPARATOR === '\\' && !(bool)env('ANSICON') && env('ConEmuANSI') !== 'ON') ||
61: (function_exists('posix_isatty') && !posix_isatty($this->_output))
62: ) {
63: $this->_defaultConfig['outputAs'] = ConsoleOutput::PLAIN;
64: } else {
65: $this->_defaultConfig['outputAs'] = ConsoleOutput::COLOR;
66: }
67:
68: parent::__construct($config);
69:
70: $config = $this->_config;
71: if ($config['stream'] instanceof ConsoleOutput) {
72: $this->_output = $config['stream'];
73: } elseif (is_string($config['stream'])) {
74: $this->_output = new ConsoleOutput($config['stream']);
75: } else {
76: throw new InvalidArgumentException('`stream` not a ConsoleOutput nor string');
77: }
78: $this->_output->setOutputAs($config['outputAs']);
79: }
80:
81: /**
82: * Implements writing to console.
83: *
84: * @param string $level The severity level of log you are making.
85: * @param string $message The message you want to log.
86: * @param array $context Additional information about the logged message
87: * @return bool success of write.
88: */
89: public function log($level, $message, array $context = [])
90: {
91: $message = $this->_format($message, $context);
92: $output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message;
93:
94: return (bool)$this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level));
95: }
96: }
97: