Updated project.json

Added Console.php
Added Utilities.php
Added Validate.php
Added \LogLib\Objects > Backtrace
Added \LogLib\Objects > Event
Added \LogLib\Objects > Options
Added \LogLib\Interfaces >  HandlerInterface
Added \LogLib\Abstracts >  LevelType.php
Added \LogLib\Abstracts >  ConsoleColors.php
Added \LogLib\Abstracts > CallType
Added project.json (NCC)
This commit is contained in:
Netkas 2022-12-13 23:21:19 -05:00
parent c77fb2da6b
commit ad7dba97d2
11 changed files with 986 additions and 0 deletions

View file

@ -0,0 +1,89 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace LogLib\Objects;
class Backtrace
{
/**
* The function name of the backtrace
*
* @var string|null
*/
public $Function;
/**
* The line number of the backtrace
*
* @var int|null
*/
public $Line;
/**
* The file name of the backtrace
*
* @var string|null
*/
public $File;
/**
* The class name, if any, of the backtrace
*
* @var string|null
*/
public $Class;
/**
* The current call type. If a method call, "->" is returned.
* If a static method call, "::" is returned. If a function call,
* nothing is returned.
*
* @see CallType
* @var string|null
*/
public $Type;
/**
* If inside a function, this lists the functions arguments. If inside
* an included file, this lists the included file name(s).
*
* @var array|null
*/
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;
}
}

View file

@ -0,0 +1,106 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace LogLib\Objects;
use LogLib\Abstracts\LevelType;
use LogLib\Classes\Utilities;
use Throwable;
class Event
{
/**
* The level of the event
*
* @see LevelType
* @var string
*/
public $Level;
/**
* The Unix Timestamp of when the event was created
*
* @var string
*/
private $Timestamp;
/**
* An array of backtraces, if any, that were created when the event was created
*
* @var Backtrace[]|null
*/
public $Backtrace;
/**
* The exception that was thrown, if any
*
* @var array|null
*/
public $Exception;
/**
* The message of the event
*
* @var string
*/
public $Message;
public function __construct()
{
$this->Timestamp = date('Y-m-dTH:i:s.v') . (date('p') == 'Z' ? 'Z' : 'L');
}
/**
* Sets an exception to the event
*
* @param Throwable $e
* @return void
*/
public function setException(Throwable $e): void
{
$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
*/
public function getTimestamp(): string
{
return $this->Timestamp;
}
}

View file

@ -0,0 +1,252 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace LogLib\Objects;
use InvalidArgumentException;
use LogLib\Abstracts\LevelType;
use LogLib\Classes\Validate;
use LogLib\Interfaces\HandlerInterface;
class Options
{
/**
* The name of the application
*
* @var string
*/
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
*/
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.
*
* @var HandlerInterface[]
*/
private $Handlers;
/**
* 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->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
{
return $this->WriteToPackageData;
}
/**
* @param bool $WriteToPackageData
*/
public function setWriteToPackageData(bool $WriteToPackageData): void
{
$this->WriteToPackageData = $WriteToPackageData;
}
/**
* @return bool
*/
public function isSplitFiles(): bool
{
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;
}
/**
* @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 = [];
}
/**
* @return string
*/
public function getApplicationName(): string
{
return $this->ApplicationName;
}
}