123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
-
-
-
- namespace Monolog;
-
- use Monolog\Handler\HandlerInterface;
- use Monolog\Handler\StreamHandler;
-
-
- class Logger
- {
-
-
- const DEBUG = 100;
-
-
-
- const INFO = 200;
-
-
-
- const WARNING = 300;
-
-
-
- const ERROR = 400;
-
-
-
- const CRITICAL = 500;
-
-
-
- const ALERT = 550;
-
- protected static $levels = array(
- 100 => 'DEBUG',
- 200 => 'INFO',
- 300 => 'WARNING',
- 400 => 'ERROR',
- 500 => 'CRITICAL',
- 550 => 'ALERT',
- );
-
- protected $name;
-
-
-
- protected $handlers = array();
-
- protected $processors = array();
-
-
-
- public function __construct($name)
- {
- $this->name = $name;
- }
-
-
-
- public function getName()
- {
- return $this->name;
- }
-
-
-
- public function pushHandler(HandlerInterface $handler)
- {
- array_unshift($this->handlers, $handler);
- }
-
-
-
- public function popHandler()
- {
- if (!$this->handlers) {
- throw new \LogicException('You tried to pop from an empty handler stack.');
- }
- return array_shift($this->handlers);
- }
-
-
-
- public function pushProcessor($callback)
- {
- if (!is_callable($callback)) {
- throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
- }
- array_unshift($this->processors, $callback);
- }
-
-
-
- public function popProcessor()
- {
- if (!$this->processors) {
- throw new \LogicException('You tried to pop from an empty processor stack.');
- }
- return array_shift($this->processors);
- }
-
-
-
- public function addRecord($level, $message, array $context = array())
- {
- if (!$this->handlers) {
- $this->pushHandler(new StreamHandler('php://stderr', self::DEBUG));
- }
- $record = array(
- 'message' => (string) $message,
- 'context' => $context,
- 'level' => $level,
- 'level_name' => self::getLevelName($level),
- 'channel' => $this->name,
- 'datetime' => new \DateTime(),
- 'extra' => array(),
- );
-
- $handlerKey = null;
- foreach ($this->handlers as $key => $handler) {
- if ($handler->isHandling($record)) {
- $handlerKey = $key;
- break;
- }
- }
-
- if (null === $handlerKey) {
- return false;
- }
-
- foreach ($this->processors as $processor) {
- $record = call_user_func($processor, $record);
- }
- while (isset($this->handlers[$handlerKey]) &&
- false === $this->handlers[$handlerKey]->handle($record)) {
- $handlerKey++;
- }
-
- return true;
- }
-
-
-
- public function addDebug($message, array $context = array())
- {
- return $this->addRecord(self::DEBUG, $message, $context);
- }
-
-
-
- public function addInfo($message, array $context = array())
- {
- return $this->addRecord(self::INFO, $message, $context);
- }
-
-
-
- public function addWarning($message, array $context = array())
- {
- return $this->addRecord(self::WARNING, $message, $context);
- }
-
-
-
- public function addError($message, array $context = array())
- {
- return $this->addRecord(self::ERROR, $message, $context);
- }
-
-
-
- public function addCritical($message, array $context = array())
- {
- return $this->addRecord(self::CRITICAL, $message, $context);
- }
-
-
-
- public function addAlert($message, array $context = array())
- {
- return $this->addRecord(self::ALERT, $message, $context);
- }
-
-
-
- public static function getLevelName($level)
- {
- return self::$levels[$level];
- }
-
-
-
-
-
- public function debug($message, array $context = array())
- {
- return $this->addRecord(self::DEBUG, $message, $context);
- }
-
-
-
- public function info($message, array $context = array())
- {
- return $this->addRecord(self::INFO, $message, $context);
- }
-
-
-
- public function notice($message, array $context = array())
- {
- return $this->addRecord(self::INFO, $message, $context);
- }
-
-
-
- public function warn($message, array $context = array())
- {
- return $this->addRecord(self::WARNING, $message, $context);
- }
-
-
-
- public function err($message, array $context = array())
- {
- return $this->addRecord(self::ERROR, $message, $context);
- }
-
-
-
- public function crit($message, array $context = array())
- {
- return $this->addRecord(self::CRITICAL, $message, $context);
- }
-
-
-
- public function alert($message, array $context = array())
- {
- return $this->addRecord(self::ALERT, $message, $context);
- }
-
-
-
- public function emerg($message, array $context = array())
- {
- return $this->addRecord(self::ALERT, $message, $context);
- }
- }
|