forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrestaShopLogger.php
179 lines (155 loc) · 5.38 KB
/
PrestaShopLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class PrestaShopLoggerCore extends ObjectModel
{
/** @var integer Log id */
public $id_log;
/** @var integer Log severity */
public $severity;
/** @var integer Error code */
public $error_code;
/** @var string Message */
public $message;
/** @var string Object type (eg. Order, Customer...) */
public $object_type;
/** @var integer Object ID */
public $object_id;
/** @var integer Object ID */
public $id_employee;
/** @var string Object creation date */
public $date_add;
/** @var string Object last modification date */
public $date_upd;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'log',
'primary' => 'id_log',
'fields' => array(
'severity' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true),
'error_code' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'message' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
'object_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'id_employee' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'object_type' => array('type' => self::TYPE_STRING, 'validate' => 'isName'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
),
);
protected static $is_present = array();
/**
* Send e-mail to the shop owner only if the minimal severity level has been reached
*
* @param Logger
* @param unknown_type $log
*/
public static function sendByMail($log)
{
if (intval(Configuration::get('PS_LOGS_BY_EMAIL')) <= intval($log->severity))
Mail::Send(
(int)Configuration::get('PS_LANG_DEFAULT'),
'log_alert',
Mail::l('Log: You have a new alert from your shop', (int)Configuration::get('PS_LANG_DEFAULT')),
array(),
Configuration::get('PS_SHOP_EMAIL')
);
}
/**
* add a log item to the database and send a mail if configured for this $severity
*
* @param string $message the log message
* @param int $severity
* @param int $error_code
* @param string $object_type
* @param int $object_id
* @param boolean $allow_duplicate if set to true, can log several time the same information (not recommended)
* @return boolean true if succeed
*/
public static function addLog($message, $severity = 1, $error_code = null, $object_type = null, $object_id = null, $allow_duplicate = false, $id_employee = null)
{
$log = new PrestaShopLogger();
$log->severity = intval($severity);
$log->error_code = intval($error_code);
$log->message = pSQL($message);
$log->date_add = date('Y-m-d H:i:s');
$log->date_upd = date('Y-m-d H:i:s');
if ($id_employee === null && isset(Context::getContext()->employee) && Validate::isLoadedObject(Context::getContext()->employee))
$id_employee = Context::getContext()->employee->id;
if ($id_employee !== null)
$log->id_employee = (int)$id_employee;
if (!empty($object_type) && !empty($object_id))
{
$log->object_type = pSQL($object_type);
$log->object_id = intval($object_id);
}
PrestaShopLogger::sendByMail($log);
if ($allow_duplicate || !$log->_isPresent())
{
$res = $log->add();
if ($res)
{
self::$is_present[$log->getHash()] = isset(self::$is_present[$log->getHash()])?self::$is_present[$log->getHash()] + 1:1;
return true;
}
}
return false;
}
/**
* this function md5($this->message.$this->severity.$this->error_code.$this->object_type.$this->object_id)
*
* @return string hash
*/
public function getHash()
{
if (empty($this->hash))
$this->hash = md5($this->message.$this->severity.$this->error_code.$this->object_type.$this->object_id);
return $this->hash;
}
public static function eraseAllLogs()
{
return Db::getInstance()->execute('TRUNCATE TABLE '._DB_PREFIX_.'log');
}
/**
* check if this log message already exists in database.
*
* @return true if exists
*/
protected function _isPresent()
{
if (!isset(self::$is_present[md5($this->message)]))
self::$is_present[$this->getHash()] = Db::getInstance()->getValue('SELECT COUNT(*)
FROM `'._DB_PREFIX_.'log`
WHERE
`message` = \''.$this->message.'\'
AND `severity` = \''.$this->severity.'\'
AND `error_code` = \''.$this->error_code.'\'
AND `object_type` = \''.$this->object_type.'\'
AND `object_id` = \''.$this->object_id.'\'
');
return self::$is_present[$this->getHash()];
}
}