/apix-log

A thin (and fast) PSR-3 logger.

Primary LanguagePHPOtherNOASSERTION

APIx Log, very thin PSR-3 logger

Latest Stable Version Build Status Code Quality Code Coverage License

Minimalist PSR-3 compliant logger.

  • Unit tested and compliant with PSR0, PSR1 and PSR2.
  • Continuously integrated against PHP 5.3, 5.4, 5.5 and 5.6.
  • Available as a Composer and as a PEAR package.

Feel free to comment, send pull requests and patches...

Basic usage (standalone)

use Apix\Log;

// Bucket for log superior or equal to `critical`
$urgent_logger = new Logger\Mail('franck@foo.bar');
$urgent_logger->setMinLevel('critical');    // set the minimal level

This logger/bucket will intercept critical, alert and emergency logs.

To log an event, use:

$urgent_logger->alert('Running out of {stuff}', ['stuff' => 'beers']);

Advanced usage (multi-logs dispatcher)

Okay. Lets create some additional loggers/buckets -- one generic, another one for development.

// Bucket for log >= to `notice`
$app_logger = new Logger\File('/var/log/apix_app.log');
$app_logger->setMinLevel('notice')
           ->setCascading(False);    // stop the log here if intercepted

// The main logger object (injecting the buckets)
$logger = new Logger( array($urgent_logger, $app_logger) );

if(DEBUG) {
  // Bucket log just for `info` and `debug`
  $debug_logger = new Logger\File('/tmp/apix_develop.log');
  $debug_logger->setMinLevel('debug');

  $logger->add($debug_logger);    // another way to inject a bucket
}

Note that setCascading() was set to False (default is True) which means that any intercepted log entries won't continue downstream past that particular bucket. So in that case, the debug bucket will only get info and debug log entries.

Finally, you can push some log entries in the following manner:

$logger->notice('Something happened -> {ctx}', array('ctx' => array(...) ) );
  
$e = New \Exception('boo!');
$logger->critical('OMG saw {exception}', [ 'exception' => $e ]);

$logger->debug($e);     // or push an object or an array directly

Log levels

The eight RFC 5424 levels of logs are supported, in order:

Severity Description
emergency System level failure (not application level)
alert Failure that requires immediate attention
critical Serious failure at the application level
error Runtime errors, used to log unhandled exceptions
warning May indicate that an error will occur if action is not taken
notice Events that are unusual but not error conditions
info Normal operational messages (no action required)
debug Verbose info useful to developers for debugging purposes

Installation

  • If you are creating a component that relies on APIx Log locally:

    • either update your composer.json file:

      {
        "require": {
          "apix/log": "1.1.*"
        }
      }
    • or update your package.xml file as follow:

      <dependencies>
        <required>
          <package>
            <name>apix_log</name>
            <channel>pear.ouarz.net</channel>
            <min>1.0.0</min>
            <max>1.999.9999</max>
          </package>
        </required>
      </dependencies>
  • For a system-wide installation using PEAR:

    sudo pear channel-discover pear.ouarz.net
    sudo pear install --alldeps ouarz/apix_log
    

For more details see pear.ouarz.net.

License

APIx Log is licensed under the New BSD license -- see the LICENSE.txt for the full license details.