ConsoleLog.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Console Logging
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  16. * @package Cake.Log.Engine
  17. * @since CakePHP(tm) v 2.2
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('BaseLog', 'Log/Engine');
  21. App::uses('ConsoleOutput', 'Console');
  22. /**
  23. * Console logging. Writes logs to console output.
  24. *
  25. * @package Cake.Log.Engine
  26. */
  27. class ConsoleLog extends BaseLog {
  28. /**
  29. * Output stream
  30. *
  31. * @var ConsoleOutput
  32. */
  33. protected $_output = null;
  34. /**
  35. * Constructs a new Console Logger.
  36. *
  37. * Config
  38. *
  39. * - `types` string or array, levels the engine is interested in
  40. * - `scopes` string or array, scopes the engine is interested in
  41. * - `stream` the path to save logs on.
  42. * - `outputAs` integer or ConsoleOutput::[RAW|PLAIN|COLOR]
  43. *
  44. * @param array $config Options for the FileLog, see above.
  45. * @throws CakeLogException
  46. */
  47. public function __construct($config = array()) {
  48. parent::__construct($config);
  49. if (DS === '\\' && !(bool)env('ANSICON')) {
  50. $outputAs = ConsoleOutput::PLAIN;
  51. } else {
  52. $outputAs = ConsoleOutput::COLOR;
  53. }
  54. $config = Hash::merge(array(
  55. 'stream' => 'php://stderr',
  56. 'types' => null,
  57. 'scopes' => array(),
  58. 'outputAs' => $outputAs,
  59. ), $this->_config);
  60. $config = $this->config($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 CakeLogException('`stream` not a ConsoleOutput nor string');
  67. }
  68. $this->_output->outputAs($config['outputAs']);
  69. }
  70. /**
  71. * Implements writing to console.
  72. *
  73. * @param string $type The type of log you are making.
  74. * @param string $message The message you want to log.
  75. * @return boolean success of write.
  76. */
  77. public function write($type, $message) {
  78. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
  79. return $this->_output->write(sprintf('<%s>%s</%s>', $type, $output, $type), false);
  80. }
  81. }