LogEngineCollection.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Registry of loaded log engines
  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://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Log
  16. * @since CakePHP(tm) v 2.2
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ObjectCollection', 'Utility');
  20. /**
  21. * Registry of loaded log engines
  22. *
  23. * @package Cake.Log
  24. */
  25. class LogEngineCollection extends ObjectCollection {
  26. /**
  27. * Loads/constructs a Log engine.
  28. *
  29. * @param string $name instance identifier
  30. * @param array $options Setting for the Log Engine
  31. * @return BaseLog BaseLog engine instance
  32. * @throws CakeLogException when logger class does not implement a write method
  33. */
  34. public function load($name, $options = array()) {
  35. $enable = isset($options['enabled']) ? $options['enabled'] : true;
  36. $loggerName = $options['engine'];
  37. unset($options['engine']);
  38. $className = $this->_getLogger($loggerName);
  39. $logger = new $className($options);
  40. if (!$logger instanceof CakeLogInterface) {
  41. throw new CakeLogException(sprintf(
  42. __d('cake_dev', 'logger class %s does not implement a write method.'), $loggerName
  43. ));
  44. }
  45. $this->_loaded[$name] = $logger;
  46. if ($enable) {
  47. $this->enable($name);
  48. }
  49. return $logger;
  50. }
  51. /**
  52. * Attempts to import a logger class from the various paths it could be on.
  53. * Checks that the logger class implements a write method as well.
  54. *
  55. * @param string $loggerName the plugin.className of the logger class you want to build.
  56. * @return mixed boolean false on any failures, string of classname to use if search was successful.
  57. * @throws CakeLogException
  58. */
  59. protected static function _getLogger($loggerName) {
  60. list($plugin, $loggerName) = pluginSplit($loggerName, true);
  61. App::uses($loggerName, $plugin . 'Log/Engine');
  62. if (!class_exists($loggerName)) {
  63. throw new CakeLogException(__d('cake_dev', 'Could not load class %s', $loggerName));
  64. }
  65. return $loggerName;
  66. }
  67. }