Created
January 24, 2014 20:20
-
-
Save andrerom/8605484 to your computer and use it in GitHub Desktop.
Abstract Logger class intended at time of writing (early 2013) for logging in new stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* File containing the Abstract ProfileLogger class | |
* | |
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved. | |
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 | |
* @version //autogentag// | |
*/ | |
namespace eZ\Publish\SPI\Profiler; | |
use eZ\Publish\SPI\Profiler\ProfilerInterface; | |
use Psr\Log\AbstractLogger; | |
use Psr\Log\LogLevel; | |
use Psr\Log\InvalidArgumentException; | |
/** | |
* ProfileLogger | |
* | |
* Generic logger that can be used for all SPi's to log information to be shown in (web) profiler. | |
*/ | |
abstract class ProfileLogger extends AbstractLogger implements ProfilerInterface | |
{ | |
/** | |
* @var int | |
*/ | |
protected $count = array(); | |
/** | |
* @var array | |
*/ | |
protected $logs = array(); | |
/** | |
* Logs with an arbitrary level. | |
* | |
* tip: Prefer to use the other functions for better code readability and less typing. | |
* | |
* @param mixed $level | |
* @param string $message | |
* @param array $context | |
* | |
* @throws \Psr\Log\InvalidArgumentException If $level is invalid | |
* @return void | |
*/ | |
final public function log( $level, $message, array $context = array() ) | |
{ | |
if ( | |
$level !== LogLevel::ALERT && | |
$level !== LogLevel::CRITICAL && | |
$level !== LogLevel::DEBUG && | |
$level !== LogLevel::EMERGENCY && | |
$level !== LogLevel::ERROR && | |
$level !== LogLevel::INFO && | |
$level !== LogLevel::NOTICE && | |
$level !== LogLevel::WARNING | |
) | |
{ | |
throw new InvalidArgumentException( "Log \$level not valid: " . $level ); | |
} | |
$this->logs[] = array( $level, $message, $context ); | |
$this->count[$level]++; | |
} | |
/** | |
* @return int | |
*/ | |
final public function getTotalCount() | |
{ | |
return array_sum( $this->count ); | |
} | |
/** | |
* @return int[] | |
*/ | |
final public function getLevelCount() | |
{ | |
return $this->count; | |
} | |
/** | |
* @return array | |
*/ | |
final public function getLogs() | |
{ | |
return $this->logs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment