ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
bool 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 ()
 {Closes the handler.This will be called automatically when the object is destroyed} More...
 
 clear ()
 Clears the buffer without flushing any messages down to the wrapped handler. More...
 
 reset ()
 
- 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
bool
} 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 ()
 
 reset ()
 
 isHandling (array $record)
 Checks whether the given record will be handled by this handler. More...
 
 handle (array $record)
 Handles a record. More...
 
 handleBatch (array $records)
 Handles a set of records at once. More...
 
 pushProcessor ($callback)
 Adds a processor in the stack. More...
 
 popProcessor ()
 Removes the processor on top of the stack and returns it. More...
 
 setFormatter (FormatterInterface $formatter)
 Sets the formatter. More...
 
 getFormatter ()
 Gets the formatter. More...
 
 reset ()
 

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
bool$bubbleWhether the messages that are handled can bubble up the stack or not

Reimplemented from Monolog\Handler\BufferHandler.

Definition at line 65 of file DeduplicationHandler.php.

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 }
static toMonologLevel($level)
Converts PSR-3 levels to Monolog ones if necessary.
Definition: Logger.php:528
const DEBUG
Detailed debug information.
Definition: Logger.php:33

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

+ 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.

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

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

+ Here is the caller graph for this function:

◆ collectLogs()

Monolog\Handler\DeduplicationHandler::collectLogs ( )
private

Definition at line 134 of file DeduplicationHandler.php.

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 }
$log
Definition: sabredav.php:21

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

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

+ Here is the caller graph for this function:

◆ flush()

Monolog\Handler\DeduplicationHandler::flush ( )

Reimplemented from Monolog\Handler\BufferHandler.

Definition at line 74 of file DeduplicationHandler.php.

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.

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

+ Here is the call graph for this function:

◆ isDuplicate()

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

Definition at line 104 of file DeduplicationHandler.php.

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 }
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
$i
Definition: disco.tpl.php:19
if(! $oauthconfig->getBoolean('getUserInfo.enable', FALSE)) $store
Definition: getUserInfo.php:11
catch(Exception $e) $message

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

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

+ 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: