Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/ncssl/source/src/Service/PluginGateway/AbstractApi.php

<?php

namespace App\Service\PluginGateway;

use App\Entity\User;
use App\Service\NcPlugin\PluginException;
use App\Service\State\StateUser;
use Monolog\Level;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Monolog\Logger;

abstract class AbstractApi implements NcApiInterface
{
    const LOG_TEMPLATE = "\ncpUser: {cpUserName}\nncUser: {ncUserName}\npid: {pid}\nMethod: {method}\nURL: {url}\nParams: {request}\nError: {error}\nResponse: {response}\n\n";
    const LOG_NO_ERROR_TEMPLATE = "\ncpUser: {cpUserName}\nncUser: {ncUserName}\npid: {pid}\nMethod: {method}\nURL: {url}\nParams: {request}\nResponse: {response}\n\n";

    protected LoggerInterface $logger;
    protected StateUser $stateUser;

    /**
     * Performs logging of data
     *
     * @param string|int|Level $logLevel One of the Logger::... level constants
     * @param array $context Data to fill in the template
     * @throws PluginException
     * @internal param string $message Template string to log data
     */
    protected function log(string|int|Level $logLevel, array $context): void
    {
        $method = strtolower(Logger::toMonologLevel($logLevel)->getName());
        if (!method_exists($this->logger, $method)) {
            throw new PluginException('Wrong log level used: ' . $logLevel);
        }

        $context['ncUserName'] = $this->stateUser->getUser()->getNcLogin();
        $context['cpUserName'] = $this->stateUser->getUser()->getName();
        $context['pid'] = getmypid();

        $template = empty($context['error']) ? static::LOG_NO_ERROR_TEMPLATE : static::LOG_TEMPLATE;

        try {
            $this->logger->{$method}($template, $context);
        } catch (\Exception $e) {
            trigger_error('Cannot write logs: ' . $e->getMessage());
        }
    }
}
Back to Directory File Manager