diff --git a/src/Logger.php b/src/Logger.php index 14fd748..e3899a0 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -76,7 +76,17 @@ public function process(LogEntry $log) } /** - * Checks if any log bucket can hanle the given code. + * Flush deferred logs + */ + public function flushDeferredLogs() + { + foreach ($this->buckets as $bucket) { + $bucket->flushDeferredLogs(); + } + } + + /** + * Checks if any log bucket can handle the given code. * * @param int $level_code * @@ -131,7 +141,7 @@ public function add(Logger\LoggerInterface $logger) /** * Sorts the log buckets, prioritizes top-down by minimal level. * Beware: Exisiting level will be in FIFO order. - * + * * @return bool Returns TRUE on success or FALSE on failure. */ protected function sortBuckets() diff --git a/src/Logger/AbstractLogger.php b/src/Logger/AbstractLogger.php index 40a25dc..c335452 100644 --- a/src/Logger/AbstractLogger.php +++ b/src/Logger/AbstractLogger.php @@ -62,6 +62,12 @@ abstract class AbstractLogger extends PsrAbstractLogger */ protected $deferred_logs = array(); + /** + * Flush deferred logs when deferred array reaches count + * @var int|null + */ + protected $deferred_trigger = null; + /** * Holds the log formatter. * @var LogFormatter|null @@ -113,6 +119,10 @@ public function process(LogEntry $log) { if ($this->deferred) { $this->deferred_logs[] = $log; + + if ($this->deferred_trigger && count($this->deferred_logs) >= $this->deferred_trigger) { + $this->flushDeferredLogs(); + } } else { $this->write($log); } @@ -212,6 +222,19 @@ public function deferred() return $this->deferred; } + /** + * Sets deferred trigger. + * + * @param int|null $value + * @return self + */ + public function setDeferredTrigger($value) + { + $this->deferred_trigger = $value; + + return $this; + } + /** * Returns all the deferred logs. * @@ -225,7 +248,7 @@ public function getDeferredLogs() /** * Process any accumulated deferred log if there are any. */ - final public function __destruct() + public function flushDeferredLogs() { if ($this->deferred && !empty($this->deferred_logs)) { @@ -235,18 +258,25 @@ function ($log) { }, $this->deferred_logs ); - + $formatter = $this->getLogFormatter(); - - $messages = implode($formatter->separator, $messages); - $entries = new LogEntry('notice', $messages); - $entries->setFormatter( $formatter ); - $this->write($entries); + $messages = implode($formatter->separator, $messages) . $formatter->separator; + + $this->write($messages); + // cleanup array + $this->deferred_logs = array(); // return $this->formatter->format($this); } + } + /** + * Process any accumulated deferred log if there are any. + */ + final public function __destruct() + { + $this->flushDeferredLogs(); $this->close(); } diff --git a/src/Logger/ErrorLog.php b/src/Logger/ErrorLog.php index 08bbec5..a293fb9 100644 --- a/src/Logger/ErrorLog.php +++ b/src/Logger/ErrorLog.php @@ -61,12 +61,12 @@ public function __construct($file = null, $type = self::PHP) /** * {@inheritDoc} */ - public function write(LogEntry $log) + public function write(LogEntry|string $log) { $message = (string) $log; if(!$this->deferred && $this->type == self::FILE) { - $message .= $log->formatter->separator; + $message = $log->formatter->separator . $message . $log->formatter->separator; } return error_log( @@ -76,5 +76,4 @@ public function write(LogEntry $log) $this->headers ); } - -} \ No newline at end of file +} diff --git a/src/Logger/File.php b/src/Logger/File.php index f732542..42743f8 100644 --- a/src/Logger/File.php +++ b/src/Logger/File.php @@ -42,5 +42,4 @@ public function __construct($file) $this->destination = $file; $this->type = static::FILE; } - } diff --git a/src/Logger/LoggerInterface.php b/src/Logger/LoggerInterface.php index 0e0852f..69d31ad 100644 --- a/src/Logger/LoggerInterface.php +++ b/src/Logger/LoggerInterface.php @@ -39,9 +39,9 @@ interface LoggerInterface /** * Writes the given log entry. * - * @param LogEntry $log + * @param LogEntry|string $log * @return bool Wether the log entry was successfully written or not. */ - public function write(LogEntry $log); + public function write(LogEntry|string $log); } diff --git a/src/Logger/Nil.php b/src/Logger/Nil.php index 3d9fa81..1663469 100644 --- a/src/Logger/Nil.php +++ b/src/Logger/Nil.php @@ -24,7 +24,7 @@ class Nil extends AbstractLogger implements LoggerInterface /** * {@inheritDoc} */ - public function write(LogEntry $log) + public function write(LogEntry|string $log) { return false; } diff --git a/src/Logger/Runtime.php b/src/Logger/Runtime.php index 7740a99..9fe9c78 100644 --- a/src/Logger/Runtime.php +++ b/src/Logger/Runtime.php @@ -28,7 +28,7 @@ class Runtime extends AbstractLogger implements LoggerInterface /** * {@inheritDoc} */ - public function write(LogEntry $log) + public function write(LogEntry|string $log) { $this->items[] = (string) $log; } diff --git a/src/Logger/Stream.php b/src/Logger/Stream.php index 221ffbd..c5c92a1 100644 --- a/src/Logger/Stream.php +++ b/src/Logger/Stream.php @@ -51,7 +51,7 @@ public function __construct($stream = 'php://stdout', $mode = 'a') /** * {@inheritDoc} */ - public function write(LogEntry $log) + public function write(LogEntry|string $log) { if (!is_resource($this->stream)) { throw new \LogicException( diff --git a/tests/InterfacesTest.php b/tests/InterfacesTest.php index 30f27f1..f0f5dbd 100644 --- a/tests/InterfacesTest.php +++ b/tests/InterfacesTest.php @@ -20,7 +20,7 @@ */ class StandardOutput extends AbstractLogger implements LoggerInterface { - public function write(LogEntry $log) + public function write(LogEntry|string $log) { echo $log; }