LogEngineCollection.php 2.3 KB

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