PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Logger.php

https://bitbucket.org/enurkov/prestashop
PHP | 163 lines | 82 code | 19 blank | 62 comment | 8 complexity | 9a8512da8c7f8a369311c2640f4ab622 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  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@prestashop.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 PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class LoggerCore extends ObjectModel
  27. {
  28. /** @var integer Log id */
  29. public $id_log;
  30. /** @var integer Log severity */
  31. public $severity;
  32. /** @var integer Error code */
  33. public $error_code;
  34. /** @var string Message */
  35. public $message;
  36. /** @var string Object type (eg. Order, Customer...) */
  37. public $object_type;
  38. /** @var integer Object ID */
  39. public $object_id;
  40. /** @var string Object creation date */
  41. public $date_add;
  42. /** @var string Object last modification date */
  43. public $date_upd;
  44. /**
  45. * @see ObjectModel::$definition
  46. */
  47. public static $definition = array(
  48. 'table' => 'log',
  49. 'primary' => 'id_log',
  50. 'fields' => array(
  51. 'severity' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true),
  52. 'error_code' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  53. 'message' => array('type' => self::TYPE_STRING, 'validate' => 'isMessage', 'required' => true),
  54. 'object_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  55. 'object_type' => array('type' => self::TYPE_STRING, 'validate' => 'isName'),
  56. 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  57. 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  58. ),
  59. );
  60. protected static $is_present = array();
  61. /**
  62. * Send e-mail to the shop owner only if the minimal severity level has been reached
  63. *
  64. * @param Logger
  65. * @param unknown_type $log
  66. */
  67. public static function sendByMail($log)
  68. {
  69. if (intval(Configuration::get('PS_LOGS_BY_EMAIL')) <= intval($log->severity))
  70. Mail::Send(
  71. (int)Configuration::get('PS_LANG_DEFAULT'),
  72. 'log_alert',
  73. Mail::l('Log: You have a new alert from your shop', (int)Configuration::get('PS_LANG_DEFAULT')),
  74. array(),
  75. Configuration::get('PS_SHOP_EMAIL')
  76. );
  77. }
  78. /**
  79. * add a log item to the database and send a mail if configured for this $severity
  80. *
  81. * @param string $message the log message
  82. * @param int $severity
  83. * @param int $error_code
  84. * @param string $object_type
  85. * @param int $object_id
  86. * @param boolean $allow_duplicate if set to true, can log several time the same information (not recommended)
  87. * @return boolean true if succeed
  88. */
  89. public static function addLog($message, $severity = 1, $error_code = null, $object_type = null, $object_id = null, $allow_duplicate = false)
  90. {
  91. $log = new Logger();
  92. $log->severity = intval($severity);
  93. $log->error_code = intval($error_code);
  94. $log->message = pSQL($message);
  95. $log->date_add = date('Y-m-d H:i:s');
  96. $log->date_upd = date('Y-m-d H:i:s');
  97. if (!empty($object_type) && !empty($object_id))
  98. {
  99. $log->object_type = pSQL($object_type);
  100. $log->object_id = intval($object_id);
  101. }
  102. Logger::sendByMail($log);
  103. if ($allow_duplicate || !$log->_isPresent())
  104. {
  105. $res = $log->add();
  106. if ($res)
  107. {
  108. self::$is_present[$log->getHash()] = isset(self::$is_present[$log->getHash()])?self::$is_present[$log->getHash()] + 1:1;
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. /**
  115. * this function md5($this->message.$this->severity.$this->error_code.$this->object_type.$this->object_id)
  116. *
  117. * @return string hash
  118. */
  119. public function getHash()
  120. {
  121. if (empty($this->hash))
  122. $this->hash = md5($this->message.$this->severity.$this->error_code.$this->object_type.$this->object_id);
  123. return $this->hash;
  124. }
  125. /**
  126. * check if this log message already exists in database.
  127. *
  128. * @return true if exists
  129. */
  130. protected function _isPresent()
  131. {
  132. if (!isset(self::$is_present[md5($this->message)]))
  133. self::$is_present[$this->getHash()] = Db::getInstance()->getValue('SELECT COUNT(*)
  134. FROM `'._DB_PREFIX_.'log`
  135. WHERE
  136. `message` = \''.$this->message.'\'
  137. AND `severity` = \''.$this->severity.'\'
  138. AND `error_code` = \''.$this->error_code.'\'
  139. AND `object_type` = \''.$this->object_type.'\'
  140. AND `object_id` = \''.$this->object_id.'\'
  141. ');
  142. return self::$is_present[$this->getHash()];
  143. }
  144. }