PHP Classes

File: docs/files/LoggerAggregatorTrait.php.txt

Recommend this page to a friend!
  Classes of Kiril Savchev   ITE Logger   docs/files/LoggerAggregatorTrait.php.txt   Download  
File: docs/files/LoggerAggregatorTrait.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: 7 years ago
Size: 3,633 bytes
 

Contents

Class file image Download
<?php /** * LoggerAggregatorTrait 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\LoggerInterface; /** * Trait for aggregating loggers * * This trait can be used for common logger aggregator functionalities * if you want to use custom class that cannot extends LoggerAggregator. * * @uses psr\log * * @ver 1.0 * * @author Kiril Savchev <k.savchev@gmail> */ trait LoggerAggregatorTrait { /** * Aggregated loggers map * * The keys of this array are log levels with arrays of * Psr\Log\LoggerInterface object ot be used when * LoggerAggreagete::log($level) is called * * @var array */ protected $loggers = []; /** * Add logger to specific level * * If level parameter is ommited than it will attached to all levels. * If level is array, then the logger will be attached to all of its * values as a level. Previously attached loggers to the same level * will be ignored. * * @param LoggerInterface $logger * @param mixed $level [Optional] Level the logger is attached to. Default NULL - to all levels */ public function attachLogger(LoggerInterface $logger, $level = '*') { if (is_array($level)) { foreach ($level as $value) { $this->attachLogger($logger, $value); } } else { if (!array_key_exists($level, $this->loggers) || !is_array($this->loggers[$level])) { $this->loggers[$level] = []; } if (in_array($logger, $this->loggers[$level])) { return true; } $this->loggers[$level][] = $logger; } } /** * Remove logger. * * You can specify a level that the logger will be remove from. If * ommitted than the logger will be entirely removed from all levels. * * @param LoggerInterface $logger * @param string $level [Optional] The level the logger be removed from. Default null */ public function detachLogger(LoggerInterface $logger, $level = null) { foreach ($this->loggers as $lvl => $loggers) { if ($level && $level !== $lvl) { continue; } if (in_array($logger, $loggers)) { $key = array_search($logger, $loggers); unset($this->loggers[$lvl][$key]); } } } /** * Attach a set of loggers in ['level' => $logger] format * * @param array $loggers */ public function setLoggers(array $loggers) { foreach ($loggers as $level => $logger) { $this->attachLogger($logger, $level); } return $this; } /** * Get the aggregated loggers * * @return array */ public function getLoggers() { return $this->loggers; } }