Announcement

Collapse
No announcement yet.

How to log messages in PHP?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to log messages in PHP?

    I added the following log for debugging purposes,[$this->logger->info("Executing formula recalculation for with IDs: " . implode(', ', $requestListIds));
    ​]
    but I cannot find it in the log file
    /volume1/web/EspoCRM-8.0.5/data/logs/espo-2024-06-16.log
    HTML Code:
        protected function recalculateFormulas()
        {
            $currentDate = new \DateTime();
    
            $requestListIds = $this->getEntityIdsWithinRange('Requestlist', 'workTime', $currentDate, 15);
            $this->logger->info("Executing formula recalculation for with IDs: " . implode(', ', $requestListIds));
            
            $arariManagementIds = $this->getEntityIdsWithinRange('ArariManagement', 'arariManagementDate', $currentDate, 15);
            $financialSettlementIds = $this->getEntityIdsWithinRange('CFinancialSettlement', 'startDate', $currentDate, 15);
    
            try {
                $this->executeRecalculateFormula('Requestlist', $requestListIds);
                $this->executeRecalculateFormula('ArariManagement', $arariManagementIds);
                $this->executeRecalculateFormula('CFinancialSettlement', $financialSettlementIds);
            } catch (\Exception $e) {
                $this->logger->error('Formula recalculation failed: ' . $e->getMessage());
            }
        }
    
        protected function getEntityIdsWithinRange($entityType, $dateField, \DateTime $currentDate, $days)
        {
            $startDate = (clone $currentDate)->modify("-$days days")->format('Y-m-d');
            $endDate = (clone $currentDate)->modify("+$days days")->format('Y-m-d');
    
            $repository = $this->entityManager->getRepository($entityType);
            $entities = $repository->where([
                $dateField => [
                    '$gte' => $startDate,
                    '$lte' => $endDate
                ]
            ])->find(['id']);
    
            $ids = [];
            foreach ($entities as $entity) {
                $ids[] = $entity->getId();
            }
    
            return $ids;
        }
    
        protected function executeRecalculateFormula($entityType, $ids)
        {
            try {
                $this->logger->info("Executing formula recalculation for $entityType with IDs: " . implode(', ', $ids));
    
                $dataFormula = [
                    'entityType' => $entityType,
                    'action' => 'recalculateFormula',
                    'params' => ['ids' => $ids]
                ];
    
                $apiUrl = "https://admin.sumking.co.jp/api/v1/MassAction";
                $ch = curl_init($apiUrl);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($dataFormula));
                $response = curl_exec($ch);
                if (curl_errno($ch)) {
                    throw new \Exception(curl_error($ch));
                }
                curl_close($ch);
    
                $responseDecoded = json_decode($response, true);
                if (isset($responseDecoded['error'])) {
                    throw new \Exce​

  • #2
    I think you need to change the debugging mode to 'info' in the config file so that your log appears. Otherwise, change 'info' to 'warning' in your file.

    Comment


    • #3
      FYI in case you are struggling with logging the context parameter: https://github.com/espocrm/espocrm/issues/3084

      Comment

      Working...
      X