PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Logger.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 166 lines | 66 code | 11 blank | 89 comment | 8 complexity | 1f5d3b6c50f616d8758a7f16b9e6adc9 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Logger model
  28. */
  29. class Mage_Core_Model_Logger
  30. {
  31. /**#@+
  32. * Keys that stand for particular log streams
  33. */
  34. const LOGGER_SYSTEM = 'system';
  35. const LOGGER_EXCEPTION = 'exception';
  36. /**#@-*/
  37. /**
  38. * @var array
  39. */
  40. protected $_loggers = array();
  41. /**
  42. * @var Mage_Core_Model_Config
  43. */
  44. protected $_config = null;
  45. /**
  46. * Instantiate with config model
  47. *
  48. * @param Mage_Core_Model_Config $config
  49. */
  50. public function __construct(Mage_Core_Model_Config $config)
  51. {
  52. $this->_config = $config;
  53. }
  54. /**
  55. * Add a logger by specified key
  56. *
  57. * Second argument is a file name (relative to log directory) or a PHP "wrapper"
  58. *
  59. * @param string $loggerKey
  60. * @param string $fileOrWrapper
  61. * @return Mage_Core_Model_Logger
  62. * @link http://php.net/wrappers
  63. */
  64. public function addStreamLog($loggerKey, $fileOrWrapper = '')
  65. {
  66. $file = $fileOrWrapper ?: "{$loggerKey}.log";
  67. if (!preg_match('#^[a-z][a-z0-9+.-]*\://#i', $file)) {
  68. $file = $this->_config->getOptions()->getLogDir() . DIRECTORY_SEPARATOR . $file;
  69. }
  70. $writerClass = (string)$this->_config->getNode('global/log/core/writer_model');
  71. if (!$writerClass || !is_subclass_of($writerClass, 'Zend_Log_Writer_Stream')) {
  72. $writerClass = 'Zend_Log_Writer_Stream';
  73. }
  74. /** @var $writer Zend_Log_Writer_Stream */
  75. $writer = $writerClass::factory(array('stream' => $file));
  76. $writer->setFormatter(
  77. new Zend_Log_Formatter_Simple('%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL)
  78. );
  79. $this->_loggers[$loggerKey] = new Zend_Log($writer);
  80. return $this;
  81. }
  82. /**
  83. * Reset all loggers and initialize them according to store configuration
  84. *
  85. * @param Mage_Core_Model_Store $store
  86. */
  87. public function initForStore(Mage_Core_Model_Store $store)
  88. {
  89. $this->_loggers = array();
  90. if ($store->getConfig('dev/log/active')) {
  91. $this->addStreamLog(self::LOGGER_SYSTEM, $store->getConfig('dev/log/file'));
  92. $this->addStreamLog(self::LOGGER_EXCEPTION, $store->getConfig('dev/log/exception_file'));
  93. }
  94. }
  95. /**
  96. * Add a logger if store configuration allows
  97. *
  98. * @param string $loggerKey
  99. * @param Mage_Core_Model_Store $store
  100. */
  101. public function addStoreLog($loggerKey, Mage_Core_Model_Store $store)
  102. {
  103. if ($store->getConfig('dev/log/active')) {
  104. $this->addStreamLog($loggerKey);
  105. }
  106. }
  107. /**
  108. * Check whether a logger exists by specified key
  109. *
  110. * @param string $key
  111. * @return bool
  112. */
  113. public function hasLog($key)
  114. {
  115. return isset($this->_loggers[$key]);
  116. }
  117. /**
  118. * Log a message
  119. *
  120. * @param string $message
  121. * @param int $level
  122. * @param string $loggerKey
  123. */
  124. public function log($message, $level = Zend_Log::DEBUG, $loggerKey = self::LOGGER_SYSTEM)
  125. {
  126. if (!isset($this->_loggers[$loggerKey])) {
  127. return;
  128. }
  129. if (is_array($message) || is_object($message)) {
  130. $message = print_r($message, true);
  131. }
  132. /** @var $logger Zend_Log */
  133. $logger = $this->_loggers[$loggerKey];
  134. $logger->log($message, $level);
  135. }
  136. /**
  137. * Log a message with "debug" level
  138. *
  139. * @param string $message
  140. * @param string $loggerKey
  141. */
  142. public function logDebug($message, $loggerKey = self::LOGGER_SYSTEM)
  143. {
  144. $this->log($message, Zend_Log::DEBUG, $loggerKey);
  145. }
  146. /**
  147. * Log an exception
  148. *
  149. * @param Exception $e
  150. */
  151. public function logException(Exception $e)
  152. {
  153. $this->log("\n" . $e->__toString(), Zend_Log::ERR, self::LOGGER_EXCEPTION);
  154. }
  155. }