PHP Classes


Recommend this page to a friend!
  Classes of Kiril Savchev  >  ITE Logger  >  >  Download  
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: ITE Logger
Log messages to different storage PSR-3 compliant
Author: By
Last change: Fix readme
Date: 3 years ago
Size: 5,875 bytes



Class file image Download

If-Then-Else Logger package


Set of classes the can be used for logging messages.

It provides simple file logging and abstract classes for sending emails with logs and writing them to database. It also has simple classes using internal php function mail() for sending logs and \PDO for writing into database tables. This packages includes LoggerAggregator class and trait that allow using of multiple nested loggers for all or different levels.

All the loggers implements PSR-3 logger standards. See for more information.


First run 'composer install' to install psr/log package. You can see the following examples in the 'example' folder

FileLogger example:


require_once '../vendor/autoload.php';

// Create the file logger with concrete log fils for 'alert' and 'error' levels:
$logger = new \Ite\Logger\FileLogger([
        'error' => '../data/logs/errors.log',
        'alert' => '../data/logs/errors.log'
// the rest of levels will use the default log file

// log 10 info messages with fake context:
for ($i=0; $i<10; $i++) {
        $logger->info("Test info {$i}",['a' => 'test context', 'qwe' => 'alabala', 'q' => [1, 2, 4]]);
// log 10 error messages with exception:
for ($i=0; $i<10; $i++) {
        $logger->error("Test error {$i}", ['exception' => new Exception("Tesitng exceptions")]);
// log 10 alert messages with exception and fake context:
for ($i=0; $i<10; $i++) {
        $logger->alert("Test alert {$i}", ['exception' => new Exception("Tesitng exceptions"), 'a' => 'test context', 'qwe' => 'alabala', 'q' => [1, 2, 4]]);

PhpMailLogger example:


require_once '../vendor/autoload.php';

use Ite\Logger\PhpMailLogger;

// Change the email addres to yours:
$logger = new PhpMailLogger('');

// Log simple info message with fake content:
$logger->info("Test info",['a' => 'test context', 'nested_context' => [1, 2, 4]]);

PdoMysqlLogger example:


require_once '../vendor/autoload.php';

$dbConfig = [
        'host' => 'localhost',
        'name' => '{{DATABASE_NAME}}', // set your own here
        'username' => '{{USERNAME}}', // set your own here
        'password' => '{{PASSWORD}}', // set your own here
        'options' => [

 * You need to execute this query to use this logger:

        message VARCHAR(255) NOT NULL,
        level VARCHAR(10) NOT NULL,
        date DATETIME NOT NULL,
        context TEXT,


$logger = new Ite\Logger\PdoMysqlLogger($dbConfig);
$logger->info("Testing database", ['key1' => 'value1']);
$logger->alert("Testing database", ['alert' => 'alerting']);

LoggerAggregator example:


require_once '../vendor/autoload.php';

use Ite\Logger\FileLogger;
use Ite\Logger\LoggerAggregator;
use Ite\Logger\PhpMailLogger;

// create some simple loggers:
$logger = new FileLogger(['error' => '../data/logs/errors.log', 'alert' => '../data/logs/errors.log']);
$logger2 = new FileLogger();
$logger3 = new FileLogger();
// Change the email addres to yours:
$emailLogger = new PhpMailLogger('');

// create Logger Aggregator with a nested logger for 'error' and 'alert' level:
$loggerAggregator = new LoggerAggregator(['error' => $logger, 'alert' => $logger]);
// attach logger for 'info', 'debug' and 'error' levels:
$loggerAggregator->attachLogger($logger2, ['info', 'debug', 'error']);
// attach logger for all log levels:
// remove a logger for 'debug' level:
$loggerAggregator->detachLogger($logger2, 'debug');
// add email logger for critical messages:
$loggerAggregator->attachLogger($emailLogger, 'critical');

// fire some loggings:
for ($i=0; $i<10; $i++) {
        $loggerAggregator->info("Test info {$i}",['a' => 'test context', 'qwe' => 'alabala']);
for ($i=0; $i<10; $i++) {
        $loggerAggregator->error("Test error {$i}", ['exception' => new Exception("Tesitng exceptions")]);
for ($i=0; $i<10; $i++) {
        $loggerAggregator->alert("Test alert {$i}", ['exception' => new Exception("Tesitng exceptions"),'a' => 'test context', 'qwe' => 'alabala']);
for ($i=0; $i<10; $i++) {
        $loggerAggregator->debug("Test debug {$i}");
// Fire criticial log message (this should send email):
$loggerAggregator->critical("Critical message");

Stream logging:


require_once '../vendor/autoload.php';

use Ite\Logger\Stream\Strategy\AbstractDatabaseLoggerStrategy;
use Ite\Logger\Stream\Strategy\AbstractEmailLoggerStrategy;
use Ite\Logger\Stream\StreamLoggerTrait;
use Ite\Logger\Stream\FileLogger;
use Ite\Logger\Stream\PhpMailLogger;
use Ite\Logger\Stream\PdoMysqlLogger;
use Psr\Log\LoggerInterface;

# Test database logger:
//stream_register_wrapper('log', PdoMysqlLogger::class);
//$stream = fopen('log://db/info', 'w');

# Test email logger:
//stream_register_wrapper('log', PhpMailLogger::class);
//$stream = fopen('log://email/info?', 'w');

# Test file logger:
stream_register_wrapper('log', FileLogger::class);
$stream = fopen('log://file/info?log=../data/logs/errors.log', 'w', STREAM_REPORT_ERRORS);

// Write log:
fwrite($stream, serialize(["Testing stream...", [1, 2, 3]]));