ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Monolog\Handler\DeduplicationHandler Class Reference

Simple handler wrapper that deduplicates log records across multiple requests. More...

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

Public Member Functions

 __construct (HandlerInterface $handler, $deduplicationStore=null, $deduplicationLevel=Logger::ERROR, $time=60, $bubble=true)
 
 flush ()
 
- Public Member Functions inherited from Monolog\Handler\BufferHandler
 __construct (HandlerInterface $handler, $bufferLimit=0, $level=Logger::DEBUG, $bubble=true, $flushOnOverflow=false)
 
 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.
} More...
 
 flush ()
 
 __destruct ()
 
 close ()
 {} More...
 
 clear ()
 Clears the buffer without flushing any messages down to the wrapped handler. 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

 $deduplicationStore
 
 $deduplicationLevel
 
 $time
 
- Protected Attributes inherited from Monolog\Handler\BufferHandler
 $handler
 
 $bufferSize = 0
 
 $bufferLimit
 
 $flushOnOverflow
 
 $buffer = array()
 
 $initialized = false
 
- Protected Attributes inherited from Monolog\Handler\AbstractHandler
 $level = Logger::DEBUG
 
 $bubble = true
 
 $formatter
 
 $processors = array()
 

Private Member Functions

 isDuplicate (array $record)
 
 collectLogs ()
 
 appendRecord (array $record)
 

Private Attributes

 $gc = false
 

Additional Inherited Members

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

Detailed Description

Simple handler wrapper that deduplicates log records across multiple requests.

It also includes the BufferHandler functionality and will buffer all messages until the end of the request or flush() is called.

This works by storing all log records' messages above $deduplicationLevel to the file specified by $deduplicationStore. When further logs come in at the end of the request (or when flush() is called), all those above $deduplicationLevel are checked against the existing stored logs. If they match and the timestamps in the stored log is not older than $time seconds, the new log record is discarded. If no log record is new, the whole data set is discarded.

This is mainly useful in combination with Mail handlers or things like Slack or HipChat handlers that send messages to people, to avoid spamming with the same message over and over in case of a major component failure like a database server being down which makes all requests fail in the same way.

Author
Jordi Boggiano j.bog.nosp@m.gian.nosp@m.o@sel.nosp@m.d.be

Definition at line 36 of file DeduplicationHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Handler\DeduplicationHandler::__construct ( HandlerInterface  $handler,
  $deduplicationStore = null,
  $deduplicationLevel = Logger::ERROR,
  $time = 60,
  $bubble = true 
)
Parameters
HandlerInterface$handlerHandler.
string$deduplicationStoreThe file/path where the deduplication log should be kept
int$deduplicationLevelThe minimum logging level for log records to be looked at for deduplication purposes
int$timeThe period (in seconds) during which duplicate entries should be suppressed after a given log is sent through
Boolean$bubbleWhether the messages that are handled can bubble up the stack or not

Definition at line 65 of file DeduplicationHandler.php.

References Monolog\Handler\AbstractHandler\$bubble, Monolog\Handler\DeduplicationHandler\$deduplicationLevel, Monolog\Handler\DeduplicationHandler\$deduplicationStore, Monolog\Handler\DeduplicationHandler\$time, Monolog\Logger\DEBUG, time, and Monolog\Logger\toMonologLevel().

66  {
67  parent::__construct($handler, 0, Logger::DEBUG, $bubble, false);
68 
69  $this->deduplicationStore = $deduplicationStore === null ? sys_get_temp_dir() . '/monolog-dedup-' . substr(md5(__FILE__), 0, 20) .'.log' : $deduplicationStore;
70  $this->deduplicationLevel = Logger::toMonologLevel($deduplicationLevel);
71  $this->time = $time;
72  }
const DEBUG
Detailed debug information.
Definition: Logger.php:32
static toMonologLevel($level)
Converts PSR-3 levels to Monolog ones if necessary.
Definition: Logger.php:473
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
+ Here is the call graph for this function:

Member Function Documentation

◆ appendRecord()

Monolog\Handler\DeduplicationHandler::appendRecord ( array  $record)
private

Definition at line 165 of file DeduplicationHandler.php.

Referenced by Monolog\Handler\DeduplicationHandler\flush().

166  {
167  file_put_contents($this->deduplicationStore, $record['datetime']->getTimestamp() . ':' . $record['level_name'] . ':' . preg_replace('{[\r\n].*}', '', $record['message']) . "\n", FILE_APPEND);
168  }
+ Here is the caller graph for this function:

◆ collectLogs()

Monolog\Handler\DeduplicationHandler::collectLogs ( )
private

Definition at line 134 of file DeduplicationHandler.php.

References $log, Monolog\Handler\DeduplicationHandler\$time, array, and time.

Referenced by Monolog\Handler\DeduplicationHandler\flush().

135  {
136  if (!file_exists($this->deduplicationStore)) {
137  return false;
138  }
139 
140  $handle = fopen($this->deduplicationStore, 'rw+');
141  flock($handle, LOCK_EX);
142  $validLogs = array();
143 
144  $timestampValidity = time() - $this->time;
145 
146  while (!feof($handle)) {
147  $log = fgets($handle);
148  if (substr($log, 0, 10) >= $timestampValidity) {
149  $validLogs[] = $log;
150  }
151  }
152 
153  ftruncate($handle, 0);
154  rewind($handle);
155  foreach ($validLogs as $log) {
156  fwrite($handle, $log);
157  }
158 
159  flock($handle, LOCK_UN);
160  fclose($handle);
161 
162  $this->gc = false;
163  }
Create styles array
The data for the language used.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
+ Here is the caller graph for this function:

◆ flush()

Monolog\Handler\DeduplicationHandler::flush ( )

Definition at line 74 of file DeduplicationHandler.php.

References Monolog\Handler\DeduplicationHandler\appendRecord(), Monolog\Handler\BufferHandler\clear(), Monolog\Handler\DeduplicationHandler\collectLogs(), and Monolog\Handler\DeduplicationHandler\isDuplicate().

75  {
76  if ($this->bufferSize === 0) {
77  return;
78  }
79 
80  $passthru = null;
81 
82  foreach ($this->buffer as $record) {
83  if ($record['level'] >= $this->deduplicationLevel) {
84 
85  $passthru = $passthru || !$this->isDuplicate($record);
86  if ($passthru) {
87  $this->appendRecord($record);
88  }
89  }
90  }
91 
92  // default of null is valid as well as if no record matches duplicationLevel we just pass through
93  if ($passthru === true || $passthru === null) {
94  $this->handler->handleBatch($this->buffer);
95  }
96 
97  $this->clear();
98 
99  if ($this->gc) {
100  $this->collectLogs();
101  }
102  }
clear()
Clears the buffer without flushing any messages down to the wrapped handler.
+ Here is the call graph for this function:

◆ isDuplicate()

Monolog\Handler\DeduplicationHandler::isDuplicate ( array  $record)
private

Definition at line 104 of file DeduplicationHandler.php.

References $i, Monolog\Handler\AbstractHandler\$level, $message, $store, Monolog\Handler\DeduplicationHandler\$time, $timestamp, file, and time.

Referenced by Monolog\Handler\DeduplicationHandler\flush().

105  {
106  if (!file_exists($this->deduplicationStore)) {
107  return false;
108  }
109 
110  $store = file($this->deduplicationStore, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
111  if (!is_array($store)) {
112  return false;
113  }
114 
115  $yesterday = time() - 86400;
116  $timestampValidity = $record['datetime']->getTimestamp() - $this->time;
117  $expectedMessage = preg_replace('{[\r\n].*}', '', $record['message']);
118 
119  for ($i = count($store) - 1; $i >= 0; $i--) {
120  list($timestamp, $level, $message) = explode(':', $store[$i], 3);
121 
122  if ($level === $record['level_name'] && $message === $expectedMessage && $timestamp > $timestampValidity) {
123  return true;
124  }
125 
126  if ($timestamp < $yesterday) {
127  $this->gc = true;
128  }
129  }
130 
131  return false;
132  }
if(! $oauthconfig->getBoolean('getUserInfo.enable', FALSE)) $store
Definition: getUserInfo.php:11
catch(Exception $e) $message
Reload workbook from saved file
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
$i
Definition: disco.tpl.php:19
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
+ Here is the caller graph for this function:

Field Documentation

◆ $deduplicationLevel

Monolog\Handler\DeduplicationHandler::$deduplicationLevel
protected

◆ $deduplicationStore

Monolog\Handler\DeduplicationHandler::$deduplicationStore
protected

◆ $gc

Monolog\Handler\DeduplicationHandler::$gc = false
private

Definition at line 56 of file DeduplicationHandler.php.

◆ $time


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