ConsoleLog.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * - `types` string or array, levels the engine is interested in
  39. * - `scopes` string or array, scopes the engine is interested in
  40. * - `stream` the path to save logs on.
  41. * - `outputAs` integer or ConsoleOutput::[RAW|PLAIN|COLOR]
  42. *
  43. * @param array $config Options for the FileLog, see above.
  44. * @throws CakeLogException
  45. */
  46. public function __construct($config = array()) {
  47. parent::__construct($config);
  48. if (DS == '\\' && !(bool)env('ANSICON')) {
  49. $outputAs = ConsoleOutput::PLAIN;
  50. } else {
  51. $outputAs = ConsoleOutput::COLOR;
  52. }
  53. $config = Hash::merge(array(
  54. 'stream' => 'php://stderr',
  55. 'types' => null,
  56. 'scopes' => array(),
  57. 'outputAs' => $outputAs,
  58. ), $this->_config);
  59. $config = $this->config($config);
  60. if ($config['stream'] instanceof ConsoleOutput) {
  61. $this->_output = $config['stream'];
  62. } elseif (is_string($config['stream'])) {
  63. $this->_output = new ConsoleOutput($config['stream']);
  64. } else {
  65. throw new CakeLogException('`stream` not a ConsoleOutput nor string');
  66. }
  67. $this->_output->outputAs($config['outputAs']);
  68. }
  69. /**
  70. * Implements writing to console.
  71. *
  72. * @param string $type The type of log you are making.
  73. * @param string $message The message you want to log.
  74. * @return boolean success of write.
  75. */
  76. public function write($type, $message) {
  77. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
  78. return $this->_output->write(sprintf('<%s>%s</%s>', $type, $output, $type), false);
  79. }
  80. }