ConsoleLog.php 2.1 KB

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