Skip to content

Commit

Permalink
Clear all logs using the log-viewer:clear command (ARCANEDEV#251)
Browse files Browse the repository at this point in the history
* basic setup for ClearLogs Command

* Feature - Clear All logs using a command. php artisan log-viewer:clear

* asks for confirmation on the execution of the command, and added the tests for it using mockey

* updated readme with new log-viewer:clear command

* Updating/Cleaning the PR ARCANEDEV#210
  • Loading branch information
arcanedev-maroc authored Dec 4, 2018
1 parent 126598d commit f222557
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 7 deletions.
5 changes: 5 additions & 0 deletions _docs/1.Installation-and-Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ php artisan log-viewer:publish --tag=lang
```bash
php artisan log-viewer:check
```
#### Clear all generated log files

```bash
php artisan log-viewer:clear
```

# DONE !

Expand Down
45 changes: 45 additions & 0 deletions src/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php namespace Arcanedev\LogViewer\Commands;

/**
* Class ClearCommand
*
* @package Arcanedev\LogViewer\Commands
* @author ARCANEDEV <[email protected]>
*/
class ClearCommand extends Command
{
/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'log-viewer:clear';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear all generated log files';

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Execute the console command.
*/
public function handle()
{
if ($this->confirm('This will delete all the log files, Do you wish to continue?')) {
$this->logViewer->clear();
$this->info('Successfully cleared the logs!');
}
}
}
7 changes: 7 additions & 0 deletions src/Contracts/LogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public function statsTable($locale = null);
*/
public function delete($date);

/**
* Clear the log files.
*
* @return bool
*/
public function clear();

/**
* List the log files.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Contracts/Utilities/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public function read($date);
*/
public function delete($date);

/**
* Clear the log files.
*
* @return bool
*/
public function clear();

/**
* Get the log file path.
*
Expand Down
10 changes: 10 additions & 0 deletions src/LogViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ public function delete($date)
return $this->filesystem->delete($date);
}

/**
* Clear the log files.
*
* @return bool
*/
public function clear()
{
return $this->filesystem->clear();
}

/**
* Get all valid log files.
*
Expand Down
1 change: 1 addition & 0 deletions src/Providers/CommandsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ class CommandsServiceProvider extends ServiceProvider
Commands\PublishCommand::class,
Commands\StatsCommand::class,
Commands\CheckCommand::class,
Commands\ClearCommand::class,
];
}
12 changes: 12 additions & 0 deletions src/Utilities/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ public function delete($date)
return true;
}

/**
* Clear the log files.
*
* @return bool
*/
public function clear()
{
return $this->filesystem->delete(
$this->logs()
);
}

/**
* Get the log file path.
*
Expand Down
90 changes: 90 additions & 0 deletions tests/Commands/ClearCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php namespace Arcanedev\LogViewer\Tests\Commands;

use Mockery;
use Arcanedev\LogViewer\Tests\TestCase;
use Arcanedev\LogViewer\Contracts\LogViewer;
use Arcanedev\LogViewer\Commands\ClearCommand;

/**
* Class ClearCommandTest
*
* @package Arcanedev\LogViewer\Tests\Commands
* @author ARCANEDEV <[email protected]>
*/
class ClearCommandTest extends TestCase
{
/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

/** @var \Arcanedev\LogViewer\LogViewer */
private $logViewer;

/** @var string */
private $path;

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

protected function setUp()
{
parent::setUp();

$this->logViewer = $this->app->make(\Arcanedev\LogViewer\Contracts\LogViewer::class);
$this->path = storage_path('logs-to-clear');

$this->setupForTests();
}

protected function tearDown()
{
rmdir($this->path);
unset($this->path);
unset($this->logViewer);

parent::tearDown();
}

/* -----------------------------------------------------------------
| Tests
| -----------------------------------------------------------------
*/

/** @test */

public function it_can_delete_all_log_files()
{
static::assertEquals(0, $this->logViewer->count());

static::createDummyLog(date('Y-m-d'), 'logs-to-clear');

static::assertEquals(1, $this->logViewer->count());

$this->artisan('log-viewer:clear')
->expectsQuestion('This will delete all the log files, Do you wish to continue?', 'yes')
->expectsOutput('Successfully cleared the logs!')
->assertExitCode(0);

static::assertEquals(0, $this->logViewer->count());
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
*/

/**
* Sets the log storage path temporarily to a new directory
*/
private function setupForTests()
{
if ( ! file_exists($this->path))
mkdir($this->path, 0777, true);

$this->logViewer->setPath($this->path);
$this->app['config']->set(['log-viewer.storage-path' => $this->path]);
}
}
2 changes: 1 addition & 1 deletion tests/LogViewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function it_can_get_log_entries_by_level()
/** @test */
public function it_can_delete_a_log_file()
{
$this->createDummyLog($date = date('Y-m-d'));
static::createDummyLog($date = date('Y-m-d'));

// Assert log exists
$entries = $this->logViewer->get($date);
Expand Down
1 change: 1 addition & 0 deletions tests/Providers/CommandsServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function it_can_provides()
\Arcanedev\LogViewer\Commands\PublishCommand::class,
\Arcanedev\LogViewer\Commands\StatsCommand::class,
\Arcanedev\LogViewer\Commands\CheckCommand::class,
\Arcanedev\LogViewer\Commands\ClearCommand::class,
];

static::assertSame($expected, $this->provider->provides());
Expand Down
6 changes: 3 additions & 3 deletions tests/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public function it_can_download_a_log_page()
/** @test */
public function it_can_delete_a_log()
{
$date = date('Y-m-d');

$this->createDummyLog($date);
static::createDummyLog(
$date = date('Y-m-d')
);

$response = $this->call('DELETE', route('log-viewer::logs.delete', compact('date')), [], [], [], ['HTTP_X-Requested-With' => 'XMLHttpRequest']);
$response->assertExactJson(['result' => 'success']);
Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,15 @@ protected static function getLogLevels()
* Create dummy log.
*
* @param string $date
* @param string $path
*
* @return bool
*/
protected function createDummyLog($date)
protected static function createDummyLog($date, $path = 'logs')
{
return copy(
storage_path('dummy.log'), // Source
storage_path("/logs/laravel-{$date}.log") // Destination
storage_path("{$path}/laravel-{$date}.log") // Destination
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Utilities/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function it_can_read_file()
/** @test */
public function it_can_delete_file()
{
$this->createDummyLog($date = date('Y-m-d'));
static::createDummyLog($date = date('Y-m-d'));

// Assert log exists
$file = $this->filesystem->read($date);
Expand Down

0 comments on commit f222557

Please sign in to comment.