1

We are using Facebook Meta Extension in github , We are getting below error in the Backend Configuration

enter image description here

Here is LogOrganisation.php

<?php
/**
 * Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved
 */

 /**
 * Helper class for generating and organizing log files.
 */
namespace Facebook\BusinessExtension\Helper;
// protected $fp;

class LogOrganization
{
    // 4096 -- Good balance for tracking backwards
    const BUFFER = 4096;

    static $criticalLines = array();

    public static function organizeLogs() {
        $arrayOfFiles = array("var/log/debug.log", "var/log/cron.log", "var/log/magento.cron.log", "var/log/system.log");
        $countCrit = 0;

        foreach($arrayOfFiles as $value) {
            if (!file_exists($value)) {
                continue;
            }

         $fp = fopen($value, 'r');
            $pos = -2; // Skip final new line character (Set to -1 if not present)
            $currentLine = '';

            while (-1 !== fseek($fp, $pos, SEEK_END)) {
                $char = fgetc($fp);
                if ("\n" == $char) {
                    if (substr($currentLine, 0, 3) == "[20") {
                        $time = strtotime(substr($currentLine, 1, 20));
                        if ($time > strtotime('-7 day')) {
                            break;
                        }

                        // Saving only critical log entries with "Facebook" phrase
                        if (strpos($currentLine, "CRITICAL") !== false &&
                            strpos($currentLine, "Facebook") !== false) {
                            self::$criticalLines[] = $currentLine;
                            $countCrit++;
                        }
                        $currentLine = '';

                        if ($countCrit == 500) {
                            break;
                        }
                    }
                } else {
                    $currentLine = $char . $currentLine;
                }
                $pos--;
            }

            if (substr($currentLine, 0, 3) == "[20") {
                $time = strtotime(substr($currentLine, 1, 20));
                if ($time > strtotime('-7 day')) {
                    break;
                }

                if (strpos($currentLine, "CRITICAL") !== false  &&
                    strpos($currentLine, "Facebook") !== false) {
                    self::$criticalLines[] = $currentLine;
                    $countCrit++;
                }
                $currentLine = '';
            }

            $countCrit = 0;
        }
        fclose($fp);

        $amuLogs = self::tailCustom("var/log/facebook-business-extension.log", 100);
        $amuLogsArr = explode("\n", $amuLogs);
        self::$criticalLines = array_merge(self::$criticalLines, $amuLogsArr);

        usort(self::$criticalLines, function ($x, $y) {
            $t1 = strtotime(substr($x, 1, 19));
            $t2 = strtotime(substr($y, 1, 19));

            return ($t1 - $t2);
        });

        return self::$criticalLines;
    }

    public static function tailCustom($filepath, $lines) {
        $f = fopen($filepath, "rb");
        if ($f === false) {
            return false;
        }

        fseek($f, -1, SEEK_END);
        if (fread($f, 1) != "\n") {
            $lines -= 1;
        }

        $output = '';
        $chunk = '';

        while (ftell($f) > 0 && $lines >=   0) {
            $seek = min(ftell($f), self::BUFFER);
            fseek($f, -$seek, SEEK_CUR);
            $output = ($chunk = fread($f, $seek)) . $output;
            fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
            $lines -= substr_count($chunk, "\n");
        }

        while ($lines++ < 0) {
            $output = substr($output, strpos($output, "\n") + 1);
        }

        fclose($f);
        return trim($output);
    }
}
5
  • Try to declare variable with null, like $fp = ""; at beginning of the function Commented Dec 28, 2023 at 12:00
  • @AzizKapasi here i got : Exception #0 (Exception): Warning: fclose() expects parameter 1 to be resource, string given in Commented Dec 28, 2023 at 12:05
  • @AzizKapasi here : pastebin.com/ZxbqM6WY i updated as you suggested Commented Dec 28, 2023 at 12:05
  • Oh I'm sorry function accepts array, so you have to declare blank array. look in yout foreach loop you are not getting any of the array value of $arrayOfFiles.. just first try to add log to this file so you can understand where and what is the issue is Commented Dec 28, 2023 at 12:09
  • @AzizKapasi Thanks for your time Commented Dec 30, 2023 at 9:45

1 Answer 1

3

You encountered this error because the variable $fp is declared inside the foreach loop, but you are trying to access it outside of the loop.

To resolve this issue, move the code fclose($fp); from line 75 to the end of the foreach loop.
Your code should look like this:

foreach($arrayOfFiles as $value) {
    if (!file_exists($value)) {
        continue;
    }

    $fp = fopen($value, 'r');
    //...other code

    $countCrit = 0;
    fclose($fp);
}
11
  • @Baby in Magento, try this Commented Dec 28, 2023 at 12:24
  • 1 exception(s): Exception #0 (Exception): Warning: fopen(var/log/facebook-business-extension.log): failed to open stream: No such file or directory in /var/www/glow-staging/vendor/facebook/facebook-for-magento2/Helper/LogOrganization.php on line 92 Commented Dec 28, 2023 at 12:29
  • here is full code : pastebin.com/3s71mWRu and error : i.sstatic.net/mXoA3.png Commented Dec 28, 2023 at 12:33
  • 1
    have you checked in var/log/ directory if facebook-business-extension.log file is available or not? Commented Dec 28, 2023 at 12:39
  • @AzizKapasi starange that no log errors related to this in log directory Commented Dec 28, 2023 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.