MathiasGrimm\LaravelLogKeeper\Support\LogUtil::getDate PHP Method

getDate() public static method

Get a Carbon date from the file name
public static getDate ( $log ) : Carbon\Carbon
$log
return Carbon\Carbon
    public static function getDate($log)
    {
        if (preg_match('/(?<date>\\d{4}-\\d{2}-\\d{2})\\.log/', $log, $matches)) {
            return new Carbon($matches['date']);
        }
        throw new InvalidArgumentException('The provided log is not in the daily format');
    }

Usage Example

 /**
  * @test
  */
 public function it_gets_the_date_as_carbon_or_throw_exception_if_it_is_not_valid_log()
 {
     $logs = ['storage/logs/laravel.log' => 'exception', 'storage/logs/laravel.log.' => 'exception', 'storage/logs/laravel-2016-03-11.log.' => '2016-03-11', 'storage/logs/laravel-2016-03-11.log.tar.gz' => '2016-03-11', 'storage/logs/laravel-2016-03-11.log.tgz' => '2016-03-11', 'storage/logs/laravel-2016-03-11.log.zip' => '2016-03-11', 'storage/logs/laravel-2016-03-11.logtmp' => '2016-03-11', 'storage/logs/laravel-2016-03-11.log' => '2016-03-11', 'storage/logs/laravel-2016-03-12.log' => '2016-03-12', 'storage/logs/laravel-2016-03-13.log' => '2016-03-13', 'storage/logs/laravel-2016-03-14.log' => '2016-03-14'];
     foreach ($logs as $log => $result) {
         $e = null;
         try {
             $date = LogUtil::getDate($log);
             $date = $date->toDateString();
             $this->assertSame($result, $date);
         } catch (\Exception $e) {
             //
         }
         if ('exception' == $result) {
             $this->assertTrue((bool) $e);
         } else {
             $this->assertFalse((bool) $e, "Expected: {$result} Obtained: {$date}");
         }
     }
 }
All Usage Examples Of MathiasGrimm\LaravelLogKeeper\Support\LogUtil::getDate