Refactored Project, various changes and bug fixes. Implemented file logging
This commit is contained in:
parent
55ec27903f
commit
09141f3d10
16 changed files with 1007 additions and 316 deletions
|
@ -10,6 +10,7 @@
|
|||
* The function name of the backtrace
|
||||
*
|
||||
* @var string|null
|
||||
* @property_name function
|
||||
*/
|
||||
public $Function;
|
||||
|
||||
|
@ -17,6 +18,7 @@
|
|||
* The line number of the backtrace
|
||||
*
|
||||
* @var int|null
|
||||
* @property_name line
|
||||
*/
|
||||
public $Line;
|
||||
|
||||
|
@ -24,6 +26,7 @@
|
|||
* The file name of the backtrace
|
||||
*
|
||||
* @var string|null
|
||||
* @property_name file
|
||||
*/
|
||||
public $File;
|
||||
|
||||
|
@ -31,6 +34,7 @@
|
|||
* The class name, if any, of the backtrace
|
||||
*
|
||||
* @var string|null
|
||||
* @property_name class
|
||||
*/
|
||||
public $Class;
|
||||
|
||||
|
@ -41,6 +45,7 @@
|
|||
*
|
||||
* @see CallType
|
||||
* @var string|null
|
||||
* @property_name type
|
||||
*/
|
||||
public $Type;
|
||||
|
||||
|
@ -49,41 +54,7 @@
|
|||
* an included file, this lists the included file name(s).
|
||||
*
|
||||
* @var array|null
|
||||
* @property_name args
|
||||
*/
|
||||
public $Args;
|
||||
|
||||
/**
|
||||
* Returns an array representation of the backtrace
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'function' => $this->Function,
|
||||
'line' => $this->Line,
|
||||
'file' => $this->File,
|
||||
'class' => $this->Class,
|
||||
'type' => $this->Type,
|
||||
'args' => $this->Args
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new DebugBacktrace object from an array representation
|
||||
*
|
||||
* @param array $array
|
||||
* @return Backtrace
|
||||
*/
|
||||
public static function fromArray(array $array): Backtrace
|
||||
{
|
||||
$backtrace = new Backtrace();
|
||||
$backtrace->Function = ($array['function'] ?? null);
|
||||
$backtrace->Line = ($array['line'] ?? null);
|
||||
$backtrace->File = ($array['file'] ?? null);
|
||||
$backtrace->Class = ($array['class'] ?? null);
|
||||
$backtrace->Type = ($array['type'] ?? null);
|
||||
$backtrace->Args = ($array['args'] ?? null);
|
||||
return $backtrace;
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
*
|
||||
* @see LevelType
|
||||
* @var string
|
||||
* @property_name level
|
||||
*/
|
||||
public $Level;
|
||||
|
||||
|
@ -22,6 +23,7 @@
|
|||
* The Unix Timestamp of when the event was created
|
||||
*
|
||||
* @var string
|
||||
* @property_name timestamp
|
||||
*/
|
||||
private $Timestamp;
|
||||
|
||||
|
@ -29,6 +31,7 @@
|
|||
* An array of backtraces, if any, that were created when the event was created
|
||||
*
|
||||
* @var Backtrace[]|null
|
||||
* @property_name backtrace
|
||||
*/
|
||||
public $Backtrace;
|
||||
|
||||
|
@ -36,6 +39,7 @@
|
|||
* The exception that was thrown, if any
|
||||
*
|
||||
* @var array|null
|
||||
* @property_name exception
|
||||
*/
|
||||
public $Exception;
|
||||
|
||||
|
@ -43,6 +47,7 @@
|
|||
* The message of the event
|
||||
*
|
||||
* @var string
|
||||
* @property_name message
|
||||
*/
|
||||
public $Message;
|
||||
|
||||
|
@ -62,39 +67,6 @@
|
|||
$this->Exception = Utilities::exceptionToArray($e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the event
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'level' => ($this->Level ?? null),
|
||||
'timestamp' => ($this->Timestamp ?? null),
|
||||
'backtrace' => $this->Backtrace,
|
||||
'exception' => $this->Exception,
|
||||
'message' => ($this->Message ?? null)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new event from an array representation
|
||||
*
|
||||
* @param array $data
|
||||
* @return Event
|
||||
*/
|
||||
public static function fromArray(array $data): Event
|
||||
{
|
||||
$event = new Event();
|
||||
$event->Level = ($data['level'] ?? null);
|
||||
$event->Timestamp = ($data['timestamp'] ?? null);
|
||||
$event->Backtrace = ($data['backtrace'] ?? null);
|
||||
$event->Exception = ($data['exception'] ?? null);
|
||||
$event->Message = ($data['message'] ?? null);
|
||||
return $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
|
99
src/LogLib/Objects/FileLogging/FileHandle.php
Normal file
99
src/LogLib/Objects/FileLogging/FileHandle.php
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
|
@ -5,9 +5,13 @@
|
|||
namespace LogLib\Objects;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use LogLib\Abstracts\LevelType;
|
||||
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
|
||||
{
|
||||
|
@ -15,62 +19,19 @@
|
|||
* The name of the application
|
||||
*
|
||||
* @var string
|
||||
* @property_name application_name
|
||||
*/
|
||||
private $ApplicationName;
|
||||
|
||||
/**
|
||||
* The name of the NCC package that is using LogLib (eg; com.example.package)
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
private $PackageName;
|
||||
|
||||
/**
|
||||
* The current output level of the logger, anything below this level will not be logged
|
||||
*
|
||||
* @see LevelType
|
||||
* @var string
|
||||
*/
|
||||
private $OutputLevel;
|
||||
|
||||
/**
|
||||
* Indicates whether the log should be written to the console or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $ConsoleOutput;
|
||||
|
||||
/**
|
||||
* Indicates whether ansi colors should be used in the console output.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $ConsoleAnsiColors;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Indicates whether the log should be split into different files based on the file size.
|
||||
* Only applies if WriteToPackageData is true.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $SplitFiles;
|
||||
|
||||
/**
|
||||
* The maximum size of a log file before it is split into a new file.
|
||||
* Only applies if WriteToPackageData is true.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $MaxFileSize;
|
||||
|
||||
/**
|
||||
* An array of handlers that wil be used to handle the log events
|
||||
* if applications want to handle the log events themselves.
|
||||
|
@ -79,136 +40,78 @@
|
|||
*/
|
||||
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 = true;
|
||||
$this->SplitFiles = true;
|
||||
$this->MaxFileSize = 1073741824; // 1GB
|
||||
$this->OutputLevel = LevelType::Info;
|
||||
$this->ConsoleOutput = true;
|
||||
$this->ConsoleAnsiColors = true;
|
||||
$this->WriteToPackageData = false;
|
||||
$this->DumpExceptions = false;
|
||||
$this->Handlers = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPackageName(): ?string
|
||||
{
|
||||
return $this->PackageName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $PackageName
|
||||
*/
|
||||
public function setPackageName(?string $PackageName): void
|
||||
{
|
||||
$this->PackageName = $PackageName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOutputLevel(): string
|
||||
{
|
||||
return $this->OutputLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $OutputLevel
|
||||
*/
|
||||
public function setOutputLevel(string $OutputLevel): void
|
||||
{
|
||||
if(!in_array($OutputLevel, LevelType::All))
|
||||
throw new InvalidArgumentException("Invalid output level provided");
|
||||
$this->OutputLevel = $OutputLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isConsoleOutput(): bool
|
||||
{
|
||||
return $this->ConsoleOutput;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $ConsoleOutput
|
||||
*/
|
||||
public function setConsoleOutput(bool $ConsoleOutput): void
|
||||
{
|
||||
$this->ConsoleOutput = $ConsoleOutput;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isConsoleAnsiColors(): bool
|
||||
{
|
||||
return $this->ConsoleAnsiColors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $ConsoleAnsiColors
|
||||
*/
|
||||
public function setConsoleAnsiColors(bool $ConsoleAnsiColors): void
|
||||
{
|
||||
$this->ConsoleAnsiColors = $ConsoleAnsiColors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isWriteToPackageData(): bool
|
||||
public function writeToPackageData(): bool
|
||||
{
|
||||
return $this->WriteToPackageData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $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 setWriteToPackageData(bool $WriteToPackageData): void
|
||||
public function enableWriteToPackageData(): void
|
||||
{
|
||||
$this->WriteToPackageData = $WriteToPackageData;
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* Disables the writing of the log to the package data path
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function isSplitFiles(): bool
|
||||
public function disableWriteToPackageData(): void
|
||||
{
|
||||
return $this->SplitFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $SplitFiles
|
||||
*/
|
||||
public function setSplitFiles(bool $SplitFiles): void
|
||||
{
|
||||
$this->SplitFiles = $SplitFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getMaxFileSize(): int
|
||||
{
|
||||
return $this->MaxFileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $MaxFileSize
|
||||
*/
|
||||
public function setMaxFileSize(int $MaxFileSize): void
|
||||
{
|
||||
if($MaxFileSize < 1)
|
||||
throw new InvalidArgumentException("Max file size must be greater than 0");
|
||||
|
||||
$this->MaxFileSize = $MaxFileSize;
|
||||
$this->WriteToPackageData = false;
|
||||
$this->PackageDataPath = null;
|
||||
unset($this->FileHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -243,10 +146,52 @@
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the Application
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getApplicationName(): string
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
153
src/LogLib/Objects/RuntimeOptions.php
Normal file
153
src/LogLib/Objects/RuntimeOptions.php
Normal file
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace LogLib\Objects;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use LogLib\Abstracts\LevelType;
|
||||
use LogLib\Classes\Utilities;
|
||||
use LogLib\Objects\FileLogging\FileHandle;
|
||||
|
||||
class RuntimeOptions
|
||||
{
|
||||
/**
|
||||
* Indicates if the console output is enabled
|
||||
*
|
||||
* @var bool
|
||||
* @property_name console_output
|
||||
*/
|
||||
private $ConsoleOutput;
|
||||
|
||||
/**
|
||||
* Indicates if ANSI colors should be used in the console output
|
||||
*
|
||||
* @var bool
|
||||
* @property_name display_ansi
|
||||
*/
|
||||
private $DisplayAnsi;
|
||||
|
||||
/**
|
||||
* Indicates if LogLib should handle uncaught exceptions
|
||||
*
|
||||
* @var bool
|
||||
* @property_name handle_exceptions
|
||||
*/
|
||||
private $HandleExceptions;
|
||||
|
||||
/**
|
||||
* Optional. The file to write the log to.
|
||||
*
|
||||
* @var string|null
|
||||
* @property_name output_log
|
||||
*/
|
||||
private $OutputLog;
|
||||
|
||||
/**
|
||||
* The current log level
|
||||
*
|
||||
* @var int
|
||||
* @see LevelType
|
||||
*/
|
||||
private $LogLevel;
|
||||
|
||||
/**
|
||||
* @var FileHandle
|
||||
*/
|
||||
private $OutputLogHandle;
|
||||
|
||||
/**
|
||||
* Public Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->ConsoleOutput = Utilities::runningInCli();
|
||||
$this->DisplayAnsi = Utilities::getDisplayAnsi();
|
||||
$this->HandleExceptions = true;
|
||||
$this->OutputLog = Utilities::getOutputLogPath();
|
||||
$this->LogLevel = Utilities::getLogLevel();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isConsoleOutput(): bool
|
||||
{
|
||||
return $this->ConsoleOutput;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $ConsoleOutput
|
||||
*/
|
||||
public function setConsoleOutput(bool $ConsoleOutput): void
|
||||
{
|
||||
$this->ConsoleOutput = $ConsoleOutput;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isDisplayAnsi(): bool
|
||||
{
|
||||
return $this->DisplayAnsi;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $DisplayAnsi
|
||||
*/
|
||||
public function setDisplayAnsi(bool $DisplayAnsi): void
|
||||
{
|
||||
$this->DisplayAnsi = $DisplayAnsi;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isHandleExceptions(): bool
|
||||
{
|
||||
return $this->HandleExceptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $HandleExceptions
|
||||
*/
|
||||
public function setHandleExceptions(bool $HandleExceptions): void
|
||||
{
|
||||
$this->HandleExceptions = $HandleExceptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLogLevel(): int
|
||||
{
|
||||
return $this->LogLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $LogLevel
|
||||
*/
|
||||
public function setLogLevel(int $LogLevel): void
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue