Example #1
0
	/**
	 * Test that archiving a log works.
	 *
	 * @depends testWrite
	 */
	public function testArchive(\Core\Utilities\Logger\LogFile $log){
		// Archive will throw exceptions if there were any problems.
		$log->archive();

		return $log;
	}
Example #2
0
	private static function _Rotate(){
		$dir = new DirectoryLocal('logs/');
		$entries = $dir->ls('log');
		foreach($entries as $file){
			/** @var \Core\Filestore\Backends\FileLocal $file */

			$log = new LogFile($file->getBasename(true));
			try{
				$log->archive();
				echo 'Archived ' . $file->getBasename(true) . ' log.' . NL;
			}
			catch(\Exception $e){
				echo $e->getMessage();
				return false;
			}
		}

		if(\ConfigHandler::Get('/core/logs/rotate/keep')){
			$entries = $dir->ls();
			$types = [];
			foreach($entries as $file){
				/** @var \Core\Filestore\Backends\FileLocal $file */

				$base = $file->getBasename();
				// I want only the filename before the first '.'.
				$base = substr($base, 0, strpos($base, '.'));

				if(!isset($types[$base])){
					$types[$base] = [];
				}

				$types[$base][ $file->getMTime() ] = $file;
			}

			foreach($types as $base => $files){
				// This will sort the files newest-to-oldest.
				krsort($files);

				$limit = \ConfigHandler::Get('/core/logs/rotate/keep');
				$x = 0;
				$deleted = 0;
				foreach($files as $file){
					/** @var \Core\Filestore\Backends\FileLocal $file */
					++$x;
					if($x > $limit){
						if($file->exists()){
							$file->delete();
							++$deleted;
						}
					}
				}

				if($deleted > 0){
					echo 'Purged ' . $deleted . ' old ' . $base  . ' log file(s).' . NL;
				}
			}
		}

		return true;
	}