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\Core\InstanceConfigTrait;
18: use Cake\Datasource\EntityInterface;
19: use JsonSerializable;
20: use Psr\Log\AbstractLogger;
21:
22: /**
23: * Base log engine class.
24: */
25: abstract class BaseLog extends AbstractLogger
26: {
27: use InstanceConfigTrait;
28:
29: /**
30: * Default config for this class
31: *
32: * @var array
33: */
34: protected $_defaultConfig = [
35: 'levels' => [],
36: 'scopes' => []
37: ];
38:
39: /**
40: * __construct method
41: *
42: * @param array $config Configuration array
43: */
44: public function __construct(array $config = [])
45: {
46: $this->setConfig($config);
47:
48: if (!is_array($this->_config['scopes']) && $this->_config['scopes'] !== false) {
49: $this->_config['scopes'] = (array)$this->_config['scopes'];
50: }
51:
52: if (!is_array($this->_config['levels'])) {
53: $this->_config['levels'] = (array)$this->_config['levels'];
54: }
55:
56: if (!empty($this->_config['types']) && empty($this->_config['levels'])) {
57: $this->_config['levels'] = (array)$this->_config['types'];
58: }
59: }
60:
61: /**
62: * Get the levels this logger is interested in.
63: *
64: * @return array
65: */
66: public function levels()
67: {
68: return $this->_config['levels'];
69: }
70:
71: /**
72: * Get the scopes this logger is interested in.
73: *
74: * @return array
75: */
76: public function scopes()
77: {
78: return $this->_config['scopes'];
79: }
80:
81: /**
82: * Converts to string the provided data so it can be logged. The context
83: * can optionally be used by log engines to interpolate variables
84: * or add additional info to the logged message.
85: *
86: * @param mixed $data The data to be converted to string and logged.
87: * @param array $context Additional logging information for the message.
88: * @return string
89: */
90: protected function _format($data, array $context = [])
91: {
92: if (is_string($data)) {
93: return $data;
94: }
95:
96: $isObject = is_object($data);
97:
98: if ($isObject && $data instanceof EntityInterface) {
99: return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
100: }
101:
102: if ($isObject && method_exists($data, '__toString')) {
103: return (string)$data;
104: }
105:
106: if ($isObject && $data instanceof JsonSerializable) {
107: return json_encode($data, JSON_UNESCAPED_UNICODE);
108: }
109:
110: return print_r($data, true);
111: }
112: }
113: