From 2e8bfebee8f563a9332afe180d2b21d23c2b2ab5 Mon Sep 17 00:00:00 2001 From: Levi Date: Sat, 26 Nov 2016 11:55:54 -0500 Subject: [PATCH] Fixing #4. Can now append to file. Most adapters won't let you append to the file, so it now reads the file and writes the file and message back. --- README.md | 4 ++-- src/Logger.php | 9 +++++++-- tests/LogFilenameTest.php | 2 +- tests/LogLevelTest.php | 2 +- tests/LoggerImplementationTest.php | 2 +- tests/WriteToFileTest.php | 6 +++--- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 01eff6f..538f1e0 100644 --- a/README.md +++ b/README.md @@ -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); @@ -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); diff --git a/src/Logger.php b/src/Logger.php index ec92667..5f4b18c 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -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; diff --git a/tests/LogFilenameTest.php b/tests/LogFilenameTest.php index 2a4b4c4..4113a4b 100644 --- a/tests/LogFilenameTest.php +++ b/tests/LogFilenameTest.php @@ -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'); diff --git a/tests/LogLevelTest.php b/tests/LogLevelTest.php index ef4ce91..aee93b1 100644 --- a/tests/LogLevelTest.php +++ b/tests/LogLevelTest.php @@ -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); diff --git a/tests/LoggerImplementationTest.php b/tests/LoggerImplementationTest.php index ed8d1d9..68895a6 100644 --- a/tests/LoggerImplementationTest.php +++ b/tests/LoggerImplementationTest.php @@ -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); diff --git a/tests/WriteToFileTest.php b/tests/WriteToFileTest.php index 648fea0..aad7d65 100644 --- a/tests/WriteToFileTest.php +++ b/tests/WriteToFileTest.php @@ -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); @@ -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); @@ -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);