BaseLog.php 3.0 KB

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