Skip to content

Commit

Permalink
Fixing #4. Can now append to file.
Browse files Browse the repository at this point in the history
Most adapters won't let you append to the file, so it now reads the file and writes the file and message back.
  • Loading branch information
levidurfee committed Nov 26, 2016
1 parent 87d1709 commit 2e8bfeb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use League\Flysystem\Adapter\Local;
use Psr\Log\LogLevel;
use wappr\Logger;

$adapter = new Local(__DIR__.'/storage/logs/', FILE_APPEND);
$adapter = new Local(__DIR__.'/storage/logs/');
$filesystem = new Filesystem($adapter);

$logger = new Logger($filesystem, LogLevel::DEBUG);
Expand All @@ -59,7 +59,7 @@ use League\Flysystem\Adapter\Local;
use Psr\Log\LogLevel;
use wappr\Logger;

$adapter = new Local(__DIR__.'/storage/logs/', FILE_APPEND);
$adapter = new Local(__DIR__.'/storage/logs/');
$filesystem = new Filesystem($adapter);

$logger = new Logger($filesystem, LogLevel::DEBUG);
Expand Down
9 changes: 7 additions & 2 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ public function log($level, $message, array $context = [])
// Create a new LogFormat instance to format the log entry.
$message = LogFormat::create($level, $message, $context);

// Get the contents of the file before writing to it. This is so it can be appended.
$contents = $this->filesystem->read($filename);
$contents = '';

// Check and see if the file exists.
if ($this->filesystem->has($filename)) {
// Get the contents of the file before writing to it. This is so it can be appended.
$contents = $this->filesystem->read($filename);
}

$contents .= $message;

Expand Down
2 changes: 1 addition & 1 deletion tests/LogFilenameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testCreateFilename()

public function testSettingPropertiesInLogger()
{
$adapter = new Local(dirname(__DIR__).'/storage/logs/', FILE_APPEND);
$adapter = new Local(dirname(__DIR__).'/storage/logs/');
$filesystem = new Filesystem($adapter);
$logger = new Logger($filesystem, Psr\Log\LogLevel::INFO);
$logger->setFilenameFormat('Y-m-d');
Expand Down
2 changes: 1 addition & 1 deletion tests/LogLevelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class LogLevelTest extends PHPUnit_Framework_TestCase
{
public function testLogLevels()
{
$adapter = new Local(dirname(__DIR__).'/storage/logs/', FILE_APPEND);
$adapter = new Local(dirname(__DIR__).'/storage/logs/');
$filesystem = new Filesystem($adapter);

$logger = new Logger($filesystem, Psr\Log\LogLevel::DEBUG);
Expand Down
2 changes: 1 addition & 1 deletion tests/LoggerImplementationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class LoggerImplementationTest extends PHPUnit_Framework_TestCase
{
public function testIfItImplementsPsrThree()
{
$adapter = new Local(dirname(__DIR__).'/storage/logs/', FILE_APPEND);
$adapter = new Local(dirname(__DIR__).'/storage/logs/');
$filesystem = new Filesystem($adapter);
$logger = new Logger($filesystem, Psr\Log\LogLevel::INFO);
$this->assertInstanceOf('\Psr\Log\LoggerInterface', $logger);
Expand Down
6 changes: 3 additions & 3 deletions tests/WriteToFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class WriteToFileTest extends PHPUnit_Framework_TestCase
{
public function testWritingToFile()
{
$adapter = new Local(dirname(__DIR__).'/storage/logs/', FILE_APPEND);
$adapter = new Local(dirname(__DIR__).'/storage/logs/');
$filesystem = new Filesystem($adapter);
$logger = new Logger($filesystem, Psr\Log\LogLevel::INFO);

Expand All @@ -33,7 +33,7 @@ public function testWritingToFile()

public function testNotWritingToFile()
{
$adapter = new Local(dirname(__DIR__).'/storage/logs/', FILE_APPEND);
$adapter = new Local(dirname(__DIR__).'/storage/logs/');
$filesystem = new Filesystem($adapter);
$logger = new Logger($filesystem, Psr\Log\LogLevel::INFO);

Expand All @@ -58,7 +58,7 @@ public function testNotWritingToFile()

public function testWritingArrayToFile()
{
$adapter = new Local(dirname(__DIR__).'/storage/logs/', FILE_APPEND);
$adapter = new Local(dirname(__DIR__).'/storage/logs/');
$filesystem = new Filesystem($adapter);
$logger = new Logger($filesystem, Psr\Log\LogLevel::INFO);

Expand Down

0 comments on commit 2e8bfeb

Please sign in to comment.