PHP Classes
elePHPant
Icontem

File: docs/files/LoggerAggregator.php.txt

Recommend this page to a friend!
  Classes of Kiril Savchev  >  ITE Logger  >  docs/files/LoggerAggregator.php.txt  >  Download  
File: docs/files/LoggerAggregator.php.txt
Role: Documentation
Content type: text/plain
Description: Documentation
Class: ITE Logger
Log messages to different storage PSR-3 compliant
Author: By
Last change:
Date: 3 years ago
Size: 2,980 bytes
 

 

Contents

Class file image Download
<?php

/**
 * LoggerAggregator file
 *
 * Copyright (c) 2016, Kiril Savchev
 * All rights reserved.
 *
 * @category Libs
 * @package Logger
 *
 * @author Kiril Savchev <k.savchev@gmail.com>
 *
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3 License
 * @link http://ifthenelse.info
 */
namespace Ite\Logger;

use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;

/**
 * LoggerAggregator
 *
 * Aggregates multiple loggers into one object and allow them to be registered
 * for a specific level of logging or for all levels.
 *
 * @uses psr\log
 *
 * @version 1.0
 *
 * @usage $loggerAggregator = new LoggerAggregator(['error' => $logger, 'alert' => $logger]);
 * @usage $loggerAggregator->attachLogger($logger2, ['info', 'debug', 'error']);
 * @usage $loggerAggregator->attachLogger($logger3);
 * @usage $loggerAggregator->detachLogger($logger2, 'debug');
 * @usage $loggerAggregator->info("Test info",['a' => 'test context']);
 * @usage $loggerAggregator->error("Test error", ['exception' => new Exception("Tesitng exceptions")]);
 * @usage $loggerAggregator->alert("Test alert", ['exception' => new Exception("Tesitng exceptions"),'a' => 'test context']);
 * @usage $loggerAggregator->debug("Test debug");
 *
 * @author Kiril Savchev <k.savchev@gmail>
 */
class LoggerAggregator extends AbstractLogger implements LoggerAggregatorInterfrace {

        use LoggerAggregatorTrait;

        const ALL_LVL = '*';

        /**
         * File aggregator
         *
         * Creates a logger aggregator with a set of loggers which log()
         * method will be triggered when the aggregator's log() is used.
         * Loggers added to the special level of '*' will be triggered
         * when every level of logging is called.
         *
         * @param array $loggers [Optional] A set of loggers in ['level' => $logger] format
         */
        public function __construct(array $loggers = []) {
                if ($loggers) {
                        $this->setLoggers($loggers);
                }
                if (!array_key_exists(self::ALL_LVL, $this->loggers)) {
                        $this->loggers[self::ALL_LVL] = [];
                }
        }

        /**
         *
         * Logs messages with specific level and context
         *
         * Fires all agregated loggers log() method
         *
         * @param string $level
         * @param string $message
         * @param array $context
         */
        public function log($level, $message, array $context = []) {
                $loggers = $this->loggers[self::ALL_LVL];
                if (array_key_exists($level, $this->loggers) && is_array($this->loggers[$level])) {
                        $loggers = array_merge($loggers, $this->loggers[$level]);
                }
                foreach ($loggers as $logger) {
                        /* @var $logger LoggerInterface */
                        $logger->log($level, $message, $context);
                }
        }

}