Various changes

This commit is contained in:
Netkas 2023-01-29 21:43:44 -05:00
parent 5fabf8df83
commit 1192db4bbf
14 changed files with 64 additions and 437 deletions

View file

@ -22,7 +22,6 @@
* The Unix Timestamp of when the event was created
*
* @var string
* @property_name timestamp
*/
private $Timestamp;
@ -30,15 +29,13 @@
* An array of backtraces, if any, that were created when the event was created
*
* @var Backtrace[]|null
* @property_name backtrace
*/
private $Backtrace;
/**
* The exception that was thrown, if any
*
* @var array|null
* @property_name exception
* @var Throwable|null
*/
public $Exception;
@ -46,10 +43,12 @@
* The message of the event
*
* @var string
* @property_name message
*/
public $Message;
/**
* Public Constructor
*/
public function __construct()
{
$this->Timestamp = date('c');

View file

@ -1,99 +0,0 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace LogLib\Objects\FileLogging;
use InvalidArgumentException;
use LogLib\Classes\Utilities;
class FileHandle
{
/**
* The file handle of the file
*
* @var resource
*/
private $resource;
/**
* The current out path where all the logs are being written to
*
* @var string
*/
private $path;
/**
* The current file path of the log file
*
* @var string
*/
private $current_file;
/**
* Public constructor
*
* @param string $path
*/
public function __construct(string $path)
{
if(is_writable($path) === false)
throw new InvalidArgumentException(sprintf('The path "%s" is not writable', $path));
$this->path = $path . DIRECTORY_SEPARATOR . 'logs';
$this->current_file = Utilities::getLogFilename();
if(!file_exists($this->current_file))
{
touch($this->current_file);
chmod($this->current_file, 0777);
}
$this->resource = fopen($this->path . DIRECTORY_SEPARATOR . $this->current_file, 'a');
if(!is_dir($this->path))
mkdir($this->path, 0777, true);
}
/**
* Writes to the file
*
* @param string $string
* @return int
*/
public function fwrite(string $string): int
{
$current_file = Utilities::getLogFilename();
if ($current_file !== $this->current_file)
{
fclose($this->resource);
$this->current_file = $current_file;
if(!file_exists($this->current_file))
{
touch($this->current_file);
chmod($this->current_file, 0777);
}
$this->resource = fopen($this->current_file, 'a');
}
return fwrite($this->resource, $string);
}
/**
* Closes the file handle
*/
public function __destruct()
{
fclose($this->resource);
}
/**
* @return false|resource
*/
public function resource()
{
return $this->resource;
}
}

View file

@ -4,14 +4,6 @@
namespace LogLib\Objects;
use InvalidArgumentException;
use LogLib\Classes\Validate;
use LogLib\Interfaces\HandlerInterface;
use LogLib\Objects\FileLogging\FileHandle;
use ncc\Exceptions\InvalidPackageNameException;
use ncc\Exceptions\InvalidScopeException;
use ncc\Exceptions\PackageLockException;
use ncc\Managers\PackageLockManager;
class Options
{
@ -23,126 +15,12 @@
*/
private $ApplicationName;
/**
* Writes the log to a file located at the package data path provided by NCC's API
* under a "logs" directory.
*
* @var bool
* @property_name write_to_package_data
*/
private $WriteToPackageData;
/**
* An array of handlers that wil be used to handle the log events
* if applications want to handle the log events themselves.
*
* @var HandlerInterface[]
*/
private $Handlers;
/**
* The file handle to write the log to if WriteToPackageData is true
*
* @var FileHandle|null
*/
private $FileHandle;
/**
* @var string|null
*/
private $PackageDataPath;
/**
* @var bool
*/
private $DumpExceptions;
/**
* Options constructor.
*/
public function __construct(string $application_name)
{
$this->ApplicationName = $application_name;
$this->WriteToPackageData = false;
$this->DumpExceptions = false;
$this->Handlers = [];
}
/**
* @return bool
*/
public function writeToPackageData(): bool
{
return $this->WriteToPackageData;
}
/**
* Enables the writing of the log to a file located at the package data path provided by NCC's API
*
* @return void
* @throws InvalidPackageNameException
* @throws InvalidScopeException
* @throws PackageLockException
*/
public function enableWriteToPackageData(): void
{
if($this->WriteToPackageData)
return;
$package_lock = new PackageLockManager();
$package = $package_lock->getPackageLock()->getPackage($this->ApplicationName);
if($package == null)
throw new InvalidArgumentException("The package data path could not be found for the package '{$this->ApplicationName}'");
$this->WriteToPackageData = true;
$this->PackageDataPath = $package->getDataPath();
if($this->FileHandle !== null)
unset($this->FileHandle);
$this->FileHandle = new FileHandle($this->PackageDataPath);
}
/**
* Disables the writing of the log to the package data path
*
* @return void
*/
public function disableWriteToPackageData(): void
{
$this->WriteToPackageData = false;
$this->PackageDataPath = null;
unset($this->FileHandle);
}
/**
* @return HandlerInterface[]
*/
public function getHandlers(): array
{
return $this->Handlers;
}
/**
* @param string $level
* @param HandlerInterface $handler
*/
public function setHandler(string $level, HandlerInterface $handler): void
{
if(!Validate::LevelType($level))
throw new InvalidArgumentException("Invalid level provided");
if(!isset($this->Handlers[$level]))
$this->Handlers[$level] = [];
$this->Handlers[$level][] = $handler;
}
/**
* @return void
*/
public function clearHandlers(): void
{
$this->Handlers = [];
}
/**
@ -155,43 +33,4 @@
return $this->ApplicationName;
}
/**
* Indicates if exceptions should be dumped to a file
*
* @return bool
*/
public function dumpExceptionsEnabled(): bool
{
return $this->DumpExceptions;
}
/**
* Enables/Disables the dumping of exceptions to the /exceptions folder of the package data path
* WriteToPackageData must be enabled for this to work properly
*
* @param bool $DumpExceptions
*/
public function setDumpExceptions(bool $DumpExceptions): void
{
if(!$this->WriteToPackageData)
throw new InvalidArgumentException('Cannot dump exceptions if WriteToPackageData is disabled');
$this->DumpExceptions = $DumpExceptions;
}
/**
* @return FileHandle|null
*/
public function getFileHandle(): ?FileHandle
{
return $this->FileHandle;
}
/**
* @return string|null
*/
public function getPackageDataPath(): ?string
{
return $this->PackageDataPath;
}
}

View file

@ -7,7 +7,6 @@
use InvalidArgumentException;
use LogLib\Abstracts\LevelType;
use LogLib\Classes\Utilities;
use LogLib\Objects\FileLogging\FileHandle;
class RuntimeOptions
{
@ -35,14 +34,6 @@
*/
private $HandleExceptions;
/**
* Optional. The file to write the log to.
*
* @var string|null
* @property_name output_log
*/
private $OutputLog;
/**
* The current log level
*
@ -51,11 +42,6 @@
*/
private $LogLevel;
/**
* @var FileHandle
*/
private $OutputLogHandle;
/**
* Public Constructor
*/
@ -64,7 +50,6 @@
$this->ConsoleOutput = Utilities::runningInCli();
$this->DisplayAnsi = Utilities::getDisplayAnsi();
$this->HandleExceptions = true;
$this->OutputLog = Utilities::getOutputLogPath();
$this->LogLevel = Utilities::getLogLevel();
}
@ -131,23 +116,4 @@
{
$this->LogLevel = $LogLevel;
}
/**
* @return ?FileHandle
*/
public function getOutputLogHandle(): ?FileHandle
{
if($this->OutputLogHandle == null)
{
if($this->OutputLog == null)
return null;
if(is_writable($this->OutputLog) === false)
throw new InvalidArgumentException(sprintf('The path "%s" is not writable', $this->OutputLog));
$this->OutputLogHandle = new FileHandle($this->OutputLog);
}
return $this->OutputLogHandle;
}
}