From 0654c911da842eef8578d0b6a1d70f2d5d8f11b4 Mon Sep 17 00:00:00 2001 From: johnykvsky Date: Tue, 23 Jan 2018 22:35:25 +0100 Subject: [PATCH] Replaced fopen/writef/fclose with file_put_contents --- src/Logger.php | 48 +++++------------------------------------------- 1 file changed, 5 insertions(+), 43 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index c3add8c..45f427c 100755 --- a/src/Logger.php +++ b/src/Logger.php @@ -80,12 +80,6 @@ class Logger extends AbstractLogger LogLevel::DEBUG => 7 ); - /** - * This holds the file handle for this instance's log file - * @var resource - */ - private $fileHandle; - /** * This holds the last line logged to the logger * Used for unit tests @@ -121,17 +115,11 @@ public function __construct($logDirectory, $logLevelThreshold = LogLevel::DEBUG, if(strpos($logDirectory, 'php://') === 0) { $this->setLogToStdOut($logDirectory); - $this->setFileHandle('w+'); } else { $this->setLogFilePath($logDirectory); if(file_exists($this->logFilePath) && !is_writable($this->logFilePath)) { throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.'); } - $this->setFileHandle('a'); - } - - if ( ! $this->fileHandle) { - throw new RuntimeException('The file could not be opened. Check permissions.'); } } @@ -158,26 +146,6 @@ public function setLogFilePath($logDirectory) { } } - /** - * @param $writeMode - * - * @internal param resource $fileHandle - */ - public function setFileHandle($writeMode) { - $this->fileHandle = fopen($this->logFilePath, $writeMode); - } - - - /** - * Class destructor - */ - public function __destruct() - { - if ($this->fileHandle) { - fclose($this->fileHandle); - } - } - /** * Sets the date format used by all instances of KLogger * @@ -223,17 +191,11 @@ public function log($level, $message, array $context = array()) */ public function write($message) { - if (null !== $this->fileHandle) { - if (fwrite($this->fileHandle, $message) === false) { - throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.'); - } else { - $this->lastLine = trim($message); - $this->logLineCount++; - - if ($this->options['flushFrequency'] && $this->logLineCount % $this->options['flushFrequency'] === 0) { - fflush($this->fileHandle); - } - } + if (file_put_contents($this->getLogFilePath(), $message, FILE_APPEND) === false) { + throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.'); + } else { + $this->lastLine = trim($message); + $this->logLineCount++; } }