Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Custom Logger

Rafal W edited this page May 20, 2017 · 4 revisions

Since, v1.7.0, PayPal PHP SDK supports custom logger injection. This allows developers to extend their own logging implementation to PHP SDK.

Steps to Implement custom logger

    use PayPal\Log\PayPalLogFactory;
    use Psr\Log\LoggerInterface;

    class MonologLogFactory implements PayPalLogFactory
    {

        /**
         * Returns logger instance implementing LoggerInterface.
         *
         * @param string $className
         * @return LoggerInterface instance of logger object implementing LoggerInterface
         */
        public function getLogger($className)
        {
            $logger = new Monolog\Logger($className);
            $logger->pushHandler(new Monolog\Handler\StreamHandler("mail.log"));

            return $logger;
        }
    }
  • Set log.AdapterFactory settings in your $apiContext->setConfig() method as shown below:
    $apiContext->setConfig(
        array(
            ...
            'log.LogEnabled' => true,
            'log.FileName' => '../PayPal.log',
            'log.LogLevel' => 'DEBUG',
            'log.AdapterFactory' => 'MonologLogFactory' // Factory class implementing \PayPal\Log\PayPalLogFactory
            ...
        )
    );
  • Thats it. Run some samples, and see the mail.log file getting created and logged with debug messages.
  • NOTE: be sure to add monolog\monolog as a dependency in composer for this example.