Logging

Last updated: August 12th, 2023
Release:

A logging system that is PSR-3 compliant but includes better functionality for controlling output.

Concept

Building on top of the Chain system, the Logger provides a PSR-3 compliant interface as well as a way to configure the way messages are output on a per-instance basis.

Since the system is PSR-3 compliant, we'll focus only on our usage of logging Appenders and how they provide easy configurable output options for your system. For information on PSR-3 logging, please refer to the PHP FIG website.

End-to-End Example

A fully-functional (and very simplistic) example of a new appender:


use Stoic\Chain\DispatchBase;
use Stoic\Log\AppenderBase;
use Stoic\Log\Logger;
use Stoic\Log\MessageDispatch;

class EchoAppender extends AppenderBase {
    public function __construct() {
        $this->setKey('EchoAppender');
        $this->setVersion('1.0.0');

        return;
    }

    public function process($sender, DispatchBase &$dispatch) {
        if (!($dispatch instanceof MessageDispatch)) {
            return;
        }

        if (count($dispatch->messages) > 0) {
            foreach (array_values($dispatch->messages) as $message) {
                echo("{$message}\n");
            }
        }

        return;
    }
}

$log = new Logger();
$log->addAppender(new EchoAppender());

$log->info("Testing log info output.");
$log->critical("Testing log critical output.");

$log->output();

Further Reading

Next Up

Continue to read about Logger, or visit the Table of Contents.