File: examples/logger_aggregator.php

Recommend this page to a friend!
  Classes of Kiril Savchev  >  ITE Logger  >  examples/logger_aggregator.php  >  Download  
File: examples/logger_aggregator.php
Role: Example script
Content type: text/plain
Description: Example script
Class: ITE Logger
Log messages to different storage PSR-3 compliant
Author: By
Last change:
Date: 4 years ago
Size: 1,677 bytes


Class file image Download

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


// 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");

For more information send a message to info at phpclasses dot org.