HEX
Server: Apache
System: Linux nc-ph-0707-26.instaforreviews.com 3.10.0-1160.119.1.el7.tuxcare.els13.x86_64 #1 SMP Fri Nov 22 06:29:45 UTC 2024 x86_64
User: qirgxuo4hkuv (1004)
PHP: 8.3.27
Disabled: NONE
Upload Files
File: /home/qirgxuo4hkuv/public_html/shovago.com/wp-content/plugins/tidio-live-chat/src/Logs/Logger.php
<?php

namespace TidioLiveChat\Logs;

if (!defined('WPINC')) {
    die('File loaded directly. Exiting.');
}


use DateTime;
use TidioLiveChat\Clock\Clock;
use function array_reverse;
use function file_exists;
use function touch;
use function sprintf;
use function error_log;
use function file_get_contents;

final class Logger
{
    /** @var string */
    private $path;
    /** @var Clock */
    private $clock;

    /**
     * @param string $path Debug logs file name
     * @param Clock $clock
     */
    public function __construct($path, $clock)
    {
        $this->path = $path;
        $this->clock = $clock;

        if (!file_exists($this->path)) {
            touch($this->path);
        }
    }

    /**
     * @param string $message
     * @return void
     */
    public function info($message)
    {
        $this->storeLogLine('info', $message);
    }

    /**
     * @param string $message
     * @return void
     */
    public function debug($message)
    {
        $this->storeLogLine('debug', $message);
    }

    /**
     * @param string $message
     * @return void
     */
    public function error($message)
    {
        $this->storeLogLine('error', $message);
    }

    /**
     * @return string
     */
    public function readLog()
    {
        $logContent = file_get_contents($this->path);
        $lines = explode(PHP_EOL, $logContent ?: '');
        $reversedLines = array_reverse($lines);
        $reversedContent = trim(implode(PHP_EOL, $reversedLines));

        return $reversedContent ?: 'Log file is empty';
    }

    /**
     * @return void
     */
    public function clearLog()
    {
        file_put_contents($this->path, '');
    }

    /**
     * @param string $severity
     * @param string $message
     * @return void
     */
    private function storeLogLine($severity, $message)
    {
        $date = $this->clock->getCurrentTimestamp()->format(DateTime::ATOM);
        $formattedError = sprintf('[%s][%s] %s' . PHP_EOL, $date, $severity, $message);
        error_log($formattedError, 3, $this->path);
    }
}