1
0
Fork 0
mirror of synced 2025-04-02 21:36:14 +03:00

Move log objects to context

This commit is contained in:
Alex Komarichev 2024-09-18 18:53:10 +03:00
parent 1f2ed2a2ee
commit 55b1eac5bd
12 changed files with 158 additions and 83 deletions

View file

@ -69,8 +69,9 @@ if (!class_exists('WC_Retailcrm_Proxy')) :
try {
WC_Retailcrm_Logger::info(
$method,
empty($arguments) ? '[no params]' : json_encode($arguments),
WC_Retailcrm_Logger::TYPE['req']
empty($arguments) ? '[no params]' : '[with arguments]',
WC_Retailcrm_Logger::TYPE['req'],
['arguments' => $arguments]
);
/** @var \WC_Retailcrm_Response $response */
$response = call_user_func_array(array($this->retailcrm, $method), $arguments);
@ -98,14 +99,16 @@ if (!class_exists('WC_Retailcrm_Proxy')) :
)) {
WC_Retailcrm_Logger::info(
$method,
'Ok [request was successful, but response is omitted]',
WC_Retailcrm_Logger::TYPE['res']
'Ok',
WC_Retailcrm_Logger::TYPE['res'],
['body' => 'request was successful, but response is omitted']
);
} else {
WC_Retailcrm_Logger::info(
$method,
'Ok ' . $response->getRawResponse(),
WC_Retailcrm_Logger::TYPE['res']
'Ok',
WC_Retailcrm_Logger::TYPE['res'],
['body' => json_decode($response->getRawResponse(), true)]
);
}

View file

@ -195,7 +195,12 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function api_sanitized($settings)
{
WC_Retailcrm_Logger::setEntry(__FUNCTION__);
WC_Retailcrm_Logger::info(__METHOD__, 'Module settings: ' . json_encode($settings));
WC_Retailcrm_Logger::info(
__METHOD__,
'Module settings',
null,
['settings' => $settings]
);
$isLoyaltyUploadPrice = false;
if (

View file

@ -183,7 +183,9 @@ if (!class_exists('WC_Retailcrm_Customers')) :
if ($this->isCustomer($customer)) {
WC_Retailcrm_Logger::info(
__METHOD__,
'WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer)
'Process WC_Customer ' . $customer->get_id(),
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
);
$this->processCustomer($customer, $order);
$response = $this->retailcrm->customersCreate($this->customer);
@ -215,7 +217,9 @@ if (!class_exists('WC_Retailcrm_Customers')) :
if ($this->isCustomer($customer)) {
WC_Retailcrm_Logger::info(
__METHOD__,
'Update WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer)
'Update WC_Customer ' . $customer->get_id(),
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
);
$this->processCustomer($customer);
$this->retailcrm->customersEdit($this->customer);
@ -244,11 +248,9 @@ if (!class_exists('WC_Retailcrm_Customers')) :
if ($this->isCustomer($customer)) {
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'Update WC_Customer: %s by CRM_Customer ID: %s',
WC_Retailcrm_Logger::formatWCObject($customer),
$crmCustomerId
)
'Update WC_Customer by CRM_Customer ID: ' . $crmCustomerId,
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
);
$this->processCustomer($customer);
$this->customer['id'] = $crmCustomerId;
@ -395,7 +397,9 @@ if (!class_exists('WC_Retailcrm_Customers')) :
{
WC_Retailcrm_Logger::info(
__METHOD__,
'Processing for upload WC_Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer)
'Processing for upload WC_Customer ' . $customer->get_id(),
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
);
$this->processCustomer($customer);
}
@ -706,7 +710,9 @@ if (!class_exists('WC_Retailcrm_Customers')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Build new customer from order data: ' . WC_Retailcrm_Logger::formatWCObject($new_customer)
'Build new customer from order data',
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($new_customer)]
);
return $new_customer;

View file

@ -127,7 +127,12 @@ if (!class_exists('WC_Retailcrm_History')) :
$builder = new WC_Retailcrm_WC_Customer_Builder();
$customers = WC_Retailcrm_History_Assembler::assemblyCustomer($history);
WC_Retailcrm_Logger::info(__METHOD__, 'Assembled customers history: ' . json_encode($customers));
WC_Retailcrm_Logger::info(
__METHOD__,
'Assembled customers history',
null,
['customers_history' => $customers]
);
WC_Retailcrm_Plugin::$history_run = true;
foreach ($customers as $crmCustomer) {
@ -181,11 +186,9 @@ if (!class_exists('WC_Retailcrm_History')) :
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'Updated WC_Customer %s: %s',
$crmCustomer['externalId'],
WC_Retailcrm_Logger::formatWCObject($wcCustomer)
)
'Updated WC_Customer ' . $crmCustomer['externalId'],
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($wcCustomer)]
);
// @codeCoverageIgnoreStart
@ -245,7 +248,12 @@ if (!class_exists('WC_Retailcrm_History')) :
$historyAssembly = WC_Retailcrm_History_Assembler::assemblyOrder($history);
WC_Retailcrm_Logger::info(__METHOD__, 'Assembled orders history: ' . json_encode($historyAssembly));
WC_Retailcrm_Logger::info(
__METHOD__,
'Assembled orders history',
null,
['orders_history' => $historyAssembly]
);
WC_Retailcrm_Plugin::$history_run = true;
foreach ($historyAssembly as $orderHistory) {
@ -309,7 +317,9 @@ if (!class_exists('WC_Retailcrm_History')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'Result WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder)
'Result WC_Order ' . $wcOrder->get_id(),
null,
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
);
}
} catch (Exception $exception) {
@ -425,7 +435,12 @@ if (!class_exists('WC_Retailcrm_History')) :
return false;
}
WC_Retailcrm_Logger::info(__METHOD__, 'Updating WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder));
WC_Retailcrm_Logger::info(
__METHOD__,
'Updating WC_Order ' . $wcOrder->get_id(),
null,
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
);
if (isset($order['status']) && isset($options[$order['status']])) {
$wcOrder->update_status($options[$order['status']]);
@ -507,7 +522,9 @@ if (!class_exists('WC_Retailcrm_History')) :
if (!$wcProduct) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf('Product %s not found by %s', json_encode($crmProduct['offer']), $this->bindField)
'Crm Product not found by ' . $this->bindField,
null,
['crm_product' => $crmProduct['offer']]
);
continue;
@ -672,7 +689,12 @@ if (!class_exists('WC_Retailcrm_History')) :
return false;
}
WC_Retailcrm_Logger::info(__METHOD__, 'Creating WC_Order from CRM_Order: ' . json_encode($order));
WC_Retailcrm_Logger::info(
__METHOD__,
'Creating WC_Order from CRM_Order: ' . $order['id'] ?? 'id empty',
null,
['crm_order' => $order]
);
if (
is_array($this->orderMethods)
@ -861,7 +883,9 @@ if (!class_exists('WC_Retailcrm_History')) :
if (!$wcProduct) {
WC_Retailcrm_Logger::error(
__METHOD__,
sprintf('Crm_Product %s not found by %s', json_encode($crmProduct['offer']), $this->bindField)
'Crm_Product not found by ' . $this->bindField,
null,
['crm_product' => $crmProduct['offer']]
);
continue;
@ -1154,10 +1178,16 @@ if (!class_exists('WC_Retailcrm_History')) :
__METHOD__,
sprintf(
'Add in order WC_Order: %s, product WC_Product: %s, CRM_Product: %s',
WC_Retailcrm_Logger::formatWCObject($wcOrder),
WC_Retailcrm_Logger::formatWCObject($wcProduct),
json_encode($crmProduct)
)
$wcOrder->get_id(),
$wcProduct->get_id(),
$crmProduct['id'] ?? 'id empty'
),
null,
[
'wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder),
'wc_product' => WC_Retailcrm_Logger::formatWCObject($wcProduct),
'crm_product' => $crmProduct,
]
);
$discountTotal = $crmProduct['discountTotal'];
$productQuantity = $crmProduct['quantity'];
@ -1212,11 +1242,12 @@ if (!class_exists('WC_Retailcrm_History')) :
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'Updating product WC_Order_Item: %s, CRM_Product: %s',
WC_Retailcrm_Logger::formatWCObject($wcOrderItem),
json_encode($crmProduct)
)
'Updating order product WC_Order_Item, CRM_Product',
null,
[
'wc_order_item' => WC_Retailcrm_Logger::formatWCObject($wcOrderItem),
'crm_product' => $crmProduct,
]
);
}
@ -1254,7 +1285,12 @@ if (!class_exists('WC_Retailcrm_History')) :
$data->setWcOrder($wcOrder);
WC_Retailcrm_Logger::info(__METHOD__, 'Processing CRM_Order ' . json_encode($order));
WC_Retailcrm_Logger::info(
__METHOD__,
'Processing CRM_Order ' . $order['id'] ?? 'id empty',
null,
['crm_order' => $order]
);
if (isset($order['customer'])) {
$crmOrder = $this->getCRMOrder($order['id'], 'id');
@ -1262,10 +1298,9 @@ if (!class_exists('WC_Retailcrm_History')) :
if (empty($crmOrder)) {
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'Cannot get order data from retailCRM. Skipping customer change. History data: %s',
json_encode($order)
)
'Cannot get order data from retailCRM. Skipping customer change.',
null,
['history_data' => $order]
);
return false;
@ -1298,10 +1333,9 @@ if (!class_exists('WC_Retailcrm_History')) :
if (empty($crmOrder)) {
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'Cannot get order data from retailCRM. Skipping customer change. History data: %s',
json_encode($order)
)
'Cannot get order data from retailCRM. Skipping customer change.',
null,
['history_data' => $order]
);
return false;
@ -1458,11 +1492,9 @@ if (!class_exists('WC_Retailcrm_History')) :
{
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'Using this individual person data in order to set it into order %s: %s',
$data->getWcOrder()->get_id(),
json_encode($crmCustomer)
)
'Using this individual person data in order to set it into order ' . $data->getWcOrder()->get_id(),
null,
['crm_customer' => $crmCustomer]
);
if ($isContact) {

View file

@ -96,7 +96,12 @@ if (!class_exists('WC_Retailcrm_Orders')) :
return null;
}
WC_Retailcrm_Logger::info(__METHOD__, 'Start order creating ' . $orderId);
WC_Retailcrm_Logger::info(
__METHOD__,
'Start order creating ' . is_int($orderId) ? $orderId : '',
null,
['wc_order' => WC_Retailcrm_Logger::formatWCObject($orderId)]
);
try {
$this->order_payment->resetData();
@ -130,7 +135,9 @@ if (!class_exists('WC_Retailcrm_Orders')) :
WC_Retailcrm_Logger::info(
__METHOD__,
'WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder)
'Create WC_Order ' . $wcOrder->get_id(),
null,
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
);
$this->processOrder($wcOrder);
@ -337,7 +344,9 @@ if (!class_exists('WC_Retailcrm_Orders')) :
$wcOrder = wc_get_order($orderId);
WC_Retailcrm_Logger::info(
__METHOD__,
'Update WC_Order: ' . WC_Retailcrm_Logger::formatWCObject($wcOrder)
'Update WC_Order ' . $wcOrder->get_id(),
null,
['wc_order' => WC_Retailcrm_Logger::formatWCObject($wcOrder)]
);
$needRecalculate = false;
@ -544,7 +553,9 @@ if (!class_exists('WC_Retailcrm_Orders')) :
foreach ($wcItems as $id => $item) {
WC_Retailcrm_Logger::info(
__METHOD__,
'Process WC_Order_Item_Product: ' . WC_Retailcrm_Logger::formatWCObject($item)
'Process WC_Order_Item_Product ' . $id,
null,
['wc_order_item_product' => WC_Retailcrm_Logger::formatWCObject($item)]
);
$crmItem = $crmItems[$id] ?? null;
$orderItems[] = $this->order_item->build($item, $crmItem)->getData();

View file

@ -43,7 +43,12 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface
{
$this->data->validate();
WC_Retailcrm_Logger::info(__METHOD__, 'Customer State: ' . json_encode($this->data));
WC_Retailcrm_Logger::info(
__METHOD__,
'Build Customer state',
null,
['customer_state' => $this->data]
);
$newCustomer = $this->data->getNewCustomer();
$newContact = $this->data->getNewContact();
@ -102,7 +107,9 @@ class WC_Retailcrm_Customer_Switcher implements WC_Retailcrm_Builder_Interface
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf('Switching customer in order %s to %s', $wcOrder->get_id(), json_encode($newCustomer))
'Switching customer in order ' . $wcOrder->get_id(),
null,
['crm_customer' => $newCustomer]
);
if (isset($newCustomer['externalId'])) {

View file

@ -119,10 +119,11 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
* @param string $method
* @param string $message
* @param null|string $type
* @param array $context
*/
public static function error(string $method, string $message, $type = null)
public static function error(string $method, string $message, $type = null, array $context = [])
{
self::log($method, $message, $type, WC_Log_Levels::ERROR);
self::log($method, $message, $type, $context, WC_Log_Levels::ERROR);
}
/**
@ -131,10 +132,11 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
* @param string $method
* @param string $message
* @param null|string $type
* @param array $context
*/
public static function info(string $method, string $message, $type = null)
public static function info(string $method, string $message, $type = null, array $context = [])
{
self::log($method, $message, $type, WC_Log_Levels::INFO);
self::log($method, $message, $type, $context, WC_Log_Levels::INFO);
}
/**
@ -143,12 +145,14 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
* @param string $method
* @param string $message
* @param string|null $type
* @param array $context
* @param string|null $level
*/
private static function log(string $method, string $message, $type = null, $level = 'info')
private static function log(string $method, string $message, $type = null, array $context = [], $level = 'info')
{
$time = self::getStartTime();
$context = ['time' => round((microtime(true) - $time), 3), 'source' => self::HANDLE];
$context['time'] = round((microtime(true) - $time), 3);
$context['source'] = self::HANDLE;
$message = sprintf(
'%s [%s] <%s> %s=> %s',
@ -162,10 +166,10 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
self::getInstance()->log($level, $message, $context);
}
public static function formatWCObject($object): string
public static function formatWCObject($object): array
{
if ($object instanceof WC_Order) {
return json_encode([
return [
'id' => $object->get_id(),
'status' => $object->get_status(),
'date_modified' => $object->get_date_modified(),
@ -186,11 +190,11 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
'email' => $object->get_billing_email(),
'payment_method_title' => $object->get_payment_method_title(),
'date_paid' => $object->get_date_paid(),
]);
];
}
if ($object instanceof WC_Customer) {
return json_encode([
return [
'id' => $object->get_id(),
'date_modified' => $object->get_date_modified(),
'email' => $object->get_email(),
@ -210,11 +214,10 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')) :
'phone' => method_exists($object, 'get_shipping_phone')
? $object->get_shipping_phone() : $object->get_billing_phone(),
],
]);
];
}
return method_exists($object, 'get_data') ?
json_encode(array_filter($object->get_data())) : json_encode($object);
return method_exists($object, 'get_data') ? (array_filter($object->get_data())) : $object;
}
}
endif;

View file

@ -35,7 +35,9 @@ class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address
} else {
WC_Retailcrm_Logger::error(
__METHOD__,
'Error Customer address is empty. Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer)
sprintf('Error: Customer %s address is empty', $customer->getId()),
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
);
}

View file

@ -53,7 +53,9 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add
} else {
WC_Retailcrm_Logger::error(
__METHOD__,
'Error Corporate Customer address is empty. Customer: ' . WC_Retailcrm_Logger::formatWCObject($customer)
sprintf('Error: Corporate Customer %s address is empty.', $customer->get_id()),
null,
['wc_customer' => WC_Retailcrm_Logger::formatWCObject($customer)]
);
}

View file

@ -155,7 +155,9 @@ class WC_Retailcrm_WC_Customer_Builder extends WC_Retailcrm_Abstract_Builder
WC_Retailcrm_Logger::info(
__METHOD__,
'Building WC_Customer from data: ' . json_encode($this->data)
'Building WC_Customer from data',
null,
['customer_data' => $this->data]
);
$this->customer->set_first_name($this->dataValue('firstName', $this->customer->get_first_name()));

View file

@ -65,11 +65,12 @@ class WC_Retailcrm_Customer_Switcher_Result
{
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'Saving WC_Customer: %s and WC_Order: %s',
WC_Retailcrm_Logger::formatWCObject($this->wcCustomer),
WC_Retailcrm_Logger::formatWCObject($this->wcOrder)
)
sprintf('Saving WC_Customer %s and WC_Order %s', $this->wcCustomer->get_id(), $this->wcOrder->get_id()),
null,
[
'wc_customer' => WC_Retailcrm_Logger::formatWCObject($this->wcCustomer),
'wc_order' => WC_Retailcrm_Logger::formatWCObject($this->wcOrder),
]
);
if (!empty($this->wcCustomer) && $this->wcCustomer->get_id()) {

View file

@ -172,11 +172,12 @@ class WC_Retailcrm_Customer_Switcher_State
if (!empty($this->newCustomer) && !empty($this->newContact)) {
WC_Retailcrm_Logger::info(
__METHOD__,
sprintf(
'State data - customer: %s and contact: %s',
json_encode($this->getNewCustomer()),
json_encode($this->getNewContact())
)
'State data - customer and contact',
null,
[
'customer' => $this->getNewCustomer(),
'contact' => $this->getNewContact(),
]
);
throw new \InvalidArgumentException(