PHP code example of php-unified / log

1. Go to this page and download the library: Download php-unified/log library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

php-unified / log example snippets


namespace YourOrganization\YourPackage;

use PhpUnified\Log\Common\LoggerInterface;

class YourClass
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * Constructor
     *
     * @param LoggerInterface $logger
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * The description of my function.
     *
     * @return void
     */
    public function myFunction(): void
    {
        //some code
        $this->logger->log(LoggerInterface::WARNING, 'Something non-severe happened.');
    }
}

namespace YourOrganization\YourPackage;

use PhpUnified\Log\Common\OptionalLoggingInterface;
use PhpUnified\Log\Common\LoggerInterface;

class YourClass implements OptionalLoggingInterface
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * Optionally set a logger after instantiation.
     *
     * @param LoggerInterface $logger
     *
     * @return void
     */
    public function setLogger(LoggerInterface $logger): void
    {
        $this->logger = $logger;
    }

    /**
     * The description of my function.
     *
     * @return void
     */
    public function myFunction(): void
    {
        //some code
        if ($this->logger !== null) {
            $this->logger->log(LoggerInterface::WARNING, 'Something non-severe happened.');
        }
    }
}