ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
Monolog\Handler\SamplingHandler Class Reference

Sampling handler. More...

+ Inheritance diagram for Monolog\Handler\SamplingHandler:
+ Collaboration diagram for Monolog\Handler\SamplingHandler:

Public Member Functions

 __construct ($handler, $factor)
 
 isHandling (array $record)
 Checks whether the given record will be handled by this handler. More...
 
 handle (array $record)
 Handles a record. More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractHandler
 __construct ($level=Logger::DEBUG, $bubble=true)
 
 isHandling (array $record)
 {Checks whether the given record will be handled by this handler.This is mostly done for performance reasons, to avoid calling processors for nothing.Handlers should still check the record levels within handle(), returning false in isHandling() is no guarantee that handle() will not be called, and isHandling() might not be called for a given record.
Parameters
array$recordPartial log record containing only a level key
Returns
Boolean
} More...
 
 handleBatch (array $records)
 {Handles a set of records at once.
Parameters
array$recordsThe records to handle (an array of record arrays)
} More...
 
 close ()
 Closes the handler. More...
 
 pushProcessor ($callback)
 {Adds a processor in the stack.
Parameters
callable$callback
Returns
self
} More...
 
 popProcessor ()
 {Removes the processor on top of the stack and returns it.
Returns
callable
} More...
 
 setFormatter (FormatterInterface $formatter)
 {Sets the formatter.
Parameters
FormatterInterface$formatter
Returns
self
} More...
 
 getFormatter ()
 {Gets the formatter.
Returns
FormatterInterface
} More...
 
 setLevel ($level)
 Sets minimum logging level at which this handler will be triggered. More...
 
 getLevel ()
 Gets minimum logging level at which this handler will be triggered. More...
 
 setBubble ($bubble)
 Sets the bubbling behavior. More...
 
 getBubble ()
 Gets the bubbling behavior. More...
 
 __destruct ()
 

Protected Attributes

 $handler
 
 $factor
 
- Protected Attributes inherited from Monolog\Handler\AbstractHandler
 $level = Logger::DEBUG
 
 $bubble = true
 
 $formatter
 
 $processors = array()
 

Additional Inherited Members

- Protected Member Functions inherited from Monolog\Handler\AbstractHandler
 getDefaultFormatter ()
 Gets the default formatter. More...
 

Detailed Description

Sampling handler.

A sampled event stream can be useful for logging high frequency events in a production environment where you only need an idea of what is happening and are not concerned with capturing every occurrence. Since the decision to handle or not handle a particular event is determined randomly, the resulting sampled log is not guaranteed to contain 1/N of the events that occurred in the application, but based on the Law of large numbers, it will tend to be close to this ratio with a large number of attempts.

Author
Bryan Davis bd808.nosp@m.@wik.nosp@m.imedi.nosp@m.a.or.nosp@m.g
Kunal Mehta legok.nosp@m.tm@g.nosp@m.mail..nosp@m.com

Definition at line 28 of file SamplingHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Handler\SamplingHandler::__construct (   $handler,
  $factor 
)
Parameters
callable | HandlerInterface$handlerHandler or factory callable($record, $fingersCrossedHandler).
int$factorSample factor

Definition at line 44 of file SamplingHandler.php.

References Monolog\Handler\SamplingHandler\$factor, and Monolog\Handler\SamplingHandler\$handler.

45  {
46  parent::__construct();
47  $this->handler = $handler;
48  $this->factor = $factor;
49 
50  if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
51  throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
52  }
53  }

Member Function Documentation

◆ handle()

Monolog\Handler\SamplingHandler::handle ( array  $record)

Handles a record.

All records may be passed to this method, and the handler should discard those that it does not want to handle.

The return value of this function controls the bubbling process of the handler stack. Unless the bubbling is interrupted (by returning true), the Logger class will keep on calling further handlers in the stack with a given log record.

Parameters
array$recordThe record to handle
Returns
Boolean true means that this handler handled the record, and that bubbling is not permitted. false means the record was either not processed or that this handler allows bubbling.

Implements Monolog\Handler\HandlerInterface.

Definition at line 60 of file SamplingHandler.php.

References Monolog\Handler\AbstractHandler\$bubble, and Monolog\Handler\SamplingHandler\isHandling().

61  {
62  if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
63  // The same logic as in FingersCrossedHandler
64  if (!$this->handler instanceof HandlerInterface) {
65  $this->handler = call_user_func($this->handler, $record, $this);
66  if (!$this->handler instanceof HandlerInterface) {
67  throw new \RuntimeException("The factory callable should return a HandlerInterface");
68  }
69  }
70 
71  if ($this->processors) {
72  foreach ($this->processors as $processor) {
73  $record = call_user_func($processor, $record);
74  }
75  }
76 
77  $this->handler->handle($record);
78  }
79 
80  return false === $this->bubble;
81  }
isHandling(array $record)
Checks whether the given record will be handled by this handler.
+ Here is the call graph for this function:

◆ isHandling()

Monolog\Handler\SamplingHandler::isHandling ( array  $record)

Checks whether the given record will be handled by this handler.

This is mostly done for performance reasons, to avoid calling processors for nothing.

Handlers should still check the record levels within handle(), returning false in isHandling() is no guarantee that handle() will not be called, and isHandling() might not be called for a given record.

Parameters
array$recordPartial log record containing only a level key
Returns
Boolean

Implements Monolog\Handler\HandlerInterface.

Definition at line 55 of file SamplingHandler.php.

Referenced by Monolog\Handler\SamplingHandler\handle().

56  {
57  return $this->handler->isHandling($record);
58  }
+ Here is the caller graph for this function:

Field Documentation

◆ $factor

int Monolog\Handler\SamplingHandler::$factor
protected

Definition at line 38 of file SamplingHandler.php.

Referenced by Monolog\Handler\SamplingHandler\__construct().

◆ $handler

callable HandlerInterface Monolog\Handler\SamplingHandler::$handler
protected

Definition at line 33 of file SamplingHandler.php.

Referenced by Monolog\Handler\SamplingHandler\__construct().


The documentation for this class was generated from the following file: