ConsoleLog.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  12. * @since 2.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Log\Engine;
  16. use Cake\Console\ConsoleOutput;
  17. use Cake\Core\Exception\Exception;
  18. use \InvalidArgumentException;
  19. /**
  20. * Console logging. Writes logs to console output.
  21. */
  22. class ConsoleLog extends BaseLog {
  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 = null;
  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 = array()) {
  54. if (DS === '\\' && !(bool)env('ANSICON')) {
  55. $this->_defaultConfig['outputAs'] = ConsoleOutput::PLAIN;
  56. } else {
  57. $this->_defaultConfig['outputAs'] = ConsoleOutput::COLOR;
  58. }
  59. parent::__construct($config);
  60. $config = $this->_config;
  61. if ($config['stream'] instanceof ConsoleOutput) {
  62. $this->_output = $config['stream'];
  63. } elseif (is_string($config['stream'])) {
  64. $this->_output = new ConsoleOutput($config['stream']);
  65. } else {
  66. throw new InvalidArgumentException('`stream` not a ConsoleOutput nor string');
  67. }
  68. $this->_output->outputAs($config['outputAs']);
  69. }
  70. /**
  71. * Implements writing to console.
  72. *
  73. * @param string $level The severity level of log you are making.
  74. * @param string $message The message you want to log.
  75. * @param string|array $scope The scope(s) a log message is being created in.
  76. * See Cake\Log\Log::config() for more information on logging scopes.
  77. * @return bool success of write.
  78. */
  79. public function write($level, $message, $scope = []) {
  80. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
  81. return $this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level), false);
  82. }
  83. }