PHP code example of phossa / phossa-logger

1. Go to this page and download the library: Download phossa/phossa-logger 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/ */

    

phossa / phossa-logger example snippets


    // logger with id 'mylogger'
    $logger = new \Phossa\Logger\Logger('mylogger');

    // a notice
    $logger->notice('a notice from code');

    // a warning
    $logger->warning('a warning from code');
    

    class InterpolateDecorator extends DecoratorAbstract
    {
        public function __invoke(LogEntryInterface $log)
        {
            // ...
        }
    }
    

    $logger->setDecorators([
        new Decorator/InterpolateDecorator(),
        function ($logEntry) {
            // ...
        }
    ]);
    

    $inter = new Decorator/InterpolateDecorator();
    $logger->addDecorator($inter);
    // ...

    // disble the InterpolateDecorator at runtime
    $inter->stopDecorator();
    

  // create a logger with channel 'MyLogger'
  $logger  = new Logger('MyLogger');

  // syslog handler with ident set to 'MyLogger'
  $syslog  = new Handler\SyslogerHandler($logger->getChannel());

  // console handler with output to stderr
  $console = new Handler\TerminalHandler();

  // add handlers
  $logger->addHandler($syslog);
  $logger->addHandler($console);

  // ...
   // at some point, stop console logging
  $console->stopHandler();
  

  // console handler with output to stderr
  $console = new Handler\TerminalHandler();

  // set AnsiFormatter
  $console->setFormatter(new Formatter\AnsiFormatter());
  

  // handler
  $syslog = new SyslogHandler();

  // set a anonymous function as a formatter
  $syslog->setFormatter(
      function ($log) {
          // ...
          return $string;
      }
  );

  // adding different handlers
  $logger->setHandlers([
      $syslog,
      function ($log) {
          // convert $log to string and send to a log device
          // ...
      }
  ]);
  

  $logger = new Logger('MyLogger', [], [],
      function ($level, $message, $context) {
          return new MyLogEntry($level, $message, $context);
      }
  );