Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deferred logs improvements #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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()
Expand Down
44 changes: 37 additions & 7 deletions src/Logger/AbstractLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*
Expand All @@ -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)) {

Expand All @@ -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();
}

Expand Down
7 changes: 3 additions & 4 deletions src/Logger/ErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -76,5 +76,4 @@ public function write(LogEntry $log)
$this->headers
);
}

}
}
1 change: 0 additions & 1 deletion src/Logger/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ public function __construct($file)
$this->destination = $file;
$this->type = static::FILE;
}

}
4 changes: 2 additions & 2 deletions src/Logger/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
2 changes: 1 addition & 1 deletion src/Logger/Nil.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Nil extends AbstractLogger implements LoggerInterface
/**
* {@inheritDoc}
*/
public function write(LogEntry $log)
public function write(LogEntry|string $log)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Logger/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Logger/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/InterfacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class StandardOutput extends AbstractLogger implements LoggerInterface
{
public function write(LogEntry $log)
public function write(LogEntry|string $log)
{
echo $log;
}
Expand Down