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

Replaced fopen/fwrite/fclose with file_put_contents #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 5 additions & 43 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.');
}
}

Expand All @@ -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
*
Expand Down Expand Up @@ -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++;
}
}

Expand Down