ConsoleLog.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Console\ConsoleOutput;
  17. use InvalidArgumentException;
  18. /**
  19. * Console logging. Writes logs to console output.
  20. */
  21. class ConsoleLog extends BaseLog
  22. {
  23. /**
  24. * Default config for this class
  25. *
  26. * @var array
  27. */
  28. protected $_defaultConfig = [
  29. 'stream' => 'php://stderr',
  30. 'levels' => null,
  31. 'scopes' => [],
  32. 'outputAs' => 'see constructor'
  33. ];
  34. /**
  35. * Output stream
  36. *
  37. * @var \Cake\Console\ConsoleOutput
  38. */
  39. protected $_output;
  40. /**
  41. * Constructs a new Console Logger.
  42. *
  43. * Config
  44. *
  45. * - `levels` string or array, levels the engine is interested in
  46. * - `scopes` string or array, scopes the engine is interested in
  47. * - `stream` the path to save logs on.
  48. * - `outputAs` integer or ConsoleOutput::[RAW|PLAIN|COLOR]
  49. *
  50. * @param array $config Options for the FileLog, see above.
  51. * @throws \InvalidArgumentException
  52. */
  53. public function __construct(array $config = [])
  54. {
  55. if ((DIRECTORY_SEPARATOR === '\\' && !(bool)env('ANSICON') && env('ConEmuANSI') !== 'ON') ||
  56. (function_exists('posix_isatty') && !posix_isatty($this->_output))
  57. ) {
  58. $this->_defaultConfig['outputAs'] = ConsoleOutput::PLAIN;
  59. } else {
  60. $this->_defaultConfig['outputAs'] = ConsoleOutput::COLOR;
  61. }
  62. parent::__construct($config);
  63. $config = $this->_config;
  64. if ($config['stream'] instanceof ConsoleOutput) {
  65. $this->_output = $config['stream'];
  66. } elseif (is_string($config['stream'])) {
  67. $this->_output = new ConsoleOutput($config['stream']);
  68. } else {
  69. throw new InvalidArgumentException('`stream` not a ConsoleOutput nor string');
  70. }
  71. $this->_output->setOutputAs($config['outputAs']);
  72. }
  73. /**
  74. * Implements writing to console.
  75. *
  76. * @param string $level The severity level of log you are making.
  77. * @param string $message The message you want to log.
  78. * @param array $context Additional information about the logged message
  79. * @return bool success of write.
  80. */
  81. public function log($level, $message, array $context = [])
  82. {
  83. $message = $this->_format($message, $context);
  84. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message;
  85. return (bool)$this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level));
  86. }
  87. }