Skip to content
Snippets Groups Projects

NullObject - 3

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Krzysztof Dyszczyk
    Edited
    nullLogger.php 1.43 KiB
    interface LoggerInterface
    {
        /**
         * System is unusable.
         *
         * @param string  $message
         * @param mixed[] $context
         *
         * @return void
         */
        public function emergency($message, array $context = array());
    
        /**
         * Detailed debug information.
         *
         * @param string  $message
         * @param mixed[] $context
         *
         * @return void
         */
        public function debug($message, array $context = array());
    
        /**
         * Logs with an arbitrary level.
         *
         * @param mixed   $level
         * @param string  $message
         * @param mixed[] $context
         *
         * @return void
         *
         * @throws \Psr\Log\InvalidArgumentException
         */
        public function log($level, $message, array $context = array());
    }
    
    class NullLogger implements LoggerInterface
    {
        /**
        * @inheritDoc
        */
        public function emergency($message, array $context = array())
        {
            $this->log(LogLevel::EMERGENCY, $message, $context);
        }
    
        /**
        * @inheritDoc
        */
        public function debug($message, array $context = array())
        {
            $this->log(LogLevel::DEBUG, $message, $context);
        }
    
        /**
        * @inheritDoc
        */
        public function log($level, $message, array $context = array())
        {
            // noop
        }
    }
    
    function get_logger(): LoggerInterface {
        try {
        // some magic code to initialize and return a logger
        } catch (Logger_Cannot_Initialize $e) {
            return new NullLogger();
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment