LogEngineCollection.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  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(
  43. __d('cake_dev', 'logger class %s does not implement a %s method.', $loggerName, 'write()')
  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. if (substr($loggerName, -3) !== 'Log') {
  63. $loggerName .= 'Log';
  64. }
  65. App::uses($loggerName, $plugin . 'Log/Engine');
  66. if (!class_exists($loggerName)) {
  67. throw new CakeLogException(__d('cake_dev', 'Could not load class %s', $loggerName));
  68. }
  69. return $loggerName;
  70. }
  71. }