diff --git a/.idea/php.xml b/.idea/php.xml index 09cee4f..cb05848 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -13,8 +13,7 @@ - - + diff --git a/Makefile b/Makefile index e664cc4..7e3ef42 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,3 @@ -debug: - ncc build --config="debug" - release: ncc build --config="release" diff --git a/src/LogLib/Abstracts/CallType.php b/src/LogLib/Abstracts/CallType.php index f3e5345..5673b50 100644 --- a/src/LogLib/Abstracts/CallType.php +++ b/src/LogLib/Abstracts/CallType.php @@ -5,8 +5,6 @@ abstract class CallType { const MethodCall = '->'; - const StaticCall = '::'; - const FunctionCall = ' '; } \ No newline at end of file diff --git a/src/LogLib/Classes/Console.php b/src/LogLib/Classes/Console.php index 44b2c18..a82531a 100644 --- a/src/LogLib/Classes/Console.php +++ b/src/LogLib/Classes/Console.php @@ -9,6 +9,7 @@ use LogLib\Log; use LogLib\Objects\Event; use LogLib\Objects\Options; + use Throwable; class Console { @@ -136,16 +137,18 @@ /** * Prints out the exception details * - * @param array $exception + * @param Throwable $exception * @return void */ - private static function outException(array $exception): void + private static function outException(Throwable $exception): void { - $trace_header = self::color($exception['file'] . ':' . $exception['line'], ConsoleColors::Purple); + $trace_header = self::color($exception->getFile() . ':' . $exception->getLine(), ConsoleColors::Purple); $trace_error = self::color('error: ', ConsoleColors::Red); - print($trace_header . ' ' . $trace_error . $exception['message'] . PHP_EOL); - print(sprintf('Error code: %s', $exception['code']) . PHP_EOL); - $trace = $exception['trace']; + + print($trace_header . ' ' . $trace_error . $exception->getMessage() . PHP_EOL); + print(sprintf('Error code: %s', $exception->getCode()) . PHP_EOL); + $trace = $exception->getTrace(); + if(count($trace) > 1) { print('Stack Trace:' . PHP_EOL); @@ -155,7 +158,7 @@ } } - if($exception['previous'] !== null) + if($exception->getPrevious() !== null) { print('Previous Exception:' . PHP_EOL); self::outException($exception['previous']); diff --git a/src/LogLib/Classes/FileLogging.php b/src/LogLib/Classes/FileLogging.php deleted file mode 100644 index b2158be..0000000 --- a/src/LogLib/Classes/FileLogging.php +++ /dev/null @@ -1,81 +0,0 @@ -getFileHandle(); - - switch($event->Level) - { - case LevelType::Debug: - case LevelType::Verbose: - if(!Validate::checkLevelType($event->Level, Log::getRuntimeOptions()->getLogLevel())) - return; - break; - - default: - break; - } - - $handle->fwrite(sprintf( - "%s [%s] [%s] (%s) %s" . PHP_EOL, - $event->getTimestamp(), - $options->getApplicationName(), - Utilities::levelToString($event->Level), - $backtrace_output !== null ? $backtrace_output : 'lambda', - $event->Message - )); - - if($event->Exception !== null) - self::dumpException($options, $event); - - if($fileHandle == null && Log::getRuntimeOptions()->getOutputLogHandle() !== null) - self::out($options, $event, Log::getRuntimeOptions()->getOutputLogHandle()); - } - - /** - * Dumps an exception to a file - * - * @param Options $options - * @param Event $event - * @return string|null - */ - public static function dumpException(Options $options, Event $event): ?string - { - if($options->dumpExceptionsEnabled() && $options->getPackageDataPath() !== null) - return null; - - $exceptions_path = $options->getPackageDataPath() . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . 'exceptions'; - if(!is_dir($exceptions_path)) - mkdir($exceptions_path, 0777, true); - - - $exception_type = str_replace('\\', '_', strtolower($event->Exception['type'])); - $exception_file = sprintf('%s_%s_%s.json', date('Y-m-d'), $exception_type, Functions::randomString(12)); - - $handle = fopen($exception_file, 'w'); - fwrite($handle, json_encode($event->Exception, JSON_PRETTY_PRINT)); - fclose($handle); - - return $exception_file; - } - } \ No newline at end of file diff --git a/src/LogLib/Classes/Utilities.php b/src/LogLib/Classes/Utilities.php index 7e34c46..dc01f2d 100644 --- a/src/LogLib/Classes/Utilities.php +++ b/src/LogLib/Classes/Utilities.php @@ -6,6 +6,7 @@ use LogLib\Objects\Backtrace; use LogLib\Objects\Event; use OptsLib\Parse; + use Throwable; class Utilities { @@ -135,22 +136,6 @@ } } - /** - * Returns the output log path from the command line arguments - * - * @return string|null - */ - public static function getOutputLogPath(): ?string - { - $args = Parse::getArguments(); - $path = ($args['log-path'] ?? $args['log-file'] ?? null); - - if($path === null) - return null; - - return $path; - } - /** * @return bool */ @@ -173,7 +158,7 @@ * * @return string */ - public static function getLogFilename() + public static function getLogFilename(): string { return date('Y-m-d') . '.log'; } @@ -223,4 +208,29 @@ return "{$class}{$type}{$function}()"; } + /** + * Returns an array representation of a throwable exception + * + * @param Throwable $e + * @return array + */ + public static function exceptionToArray(Throwable $e): array + { + $trace = $e->getTrace(); + $trace_string = ''; + + foreach($trace as $t) + { + $trace_string .= "\t{$t['file']}:{$t['line']} {$t['class']}{$t['type']}{$t['function']}()\n"; + } + + return [ + 'message' => $e->getMessage(), + 'code' => $e->getCode(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'trace' => $trace_string, + ]; + } + } \ No newline at end of file diff --git a/src/LogLib/Interfaces/HandlerInterface.php b/src/LogLib/Interfaces/HandlerInterface.php deleted file mode 100644 index 69d6098..0000000 --- a/src/LogLib/Interfaces/HandlerInterface.php +++ /dev/null @@ -1,10 +0,0 @@ -isConsoleOutput()) Console::out($application, $event); - - if($application->writeToPackageData()) - FileLogging::out($application, $event); - - foreach($application->getHandlers() as $event_level => $handlers) - { - if(Validate::checkLevelType($event_level, $level)) - { - foreach($handlers as $handler) - $handler->handle($event); - } - } } /** @@ -206,7 +192,7 @@ public static function registerExceptionHandler(): void { set_exception_handler(function(Throwable $throwable) { - self::error('Exception', $throwable->getMessage(), $throwable); + self::error('Runtime', $throwable->getMessage(), $throwable); }); } diff --git a/src/LogLib/Objects/Event.php b/src/LogLib/Objects/Event.php index dbcc70f..69ef4ea 100644 --- a/src/LogLib/Objects/Event.php +++ b/src/LogLib/Objects/Event.php @@ -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'); diff --git a/src/LogLib/Objects/FileLogging/FileHandle.php b/src/LogLib/Objects/FileLogging/FileHandle.php deleted file mode 100644 index 1d9f11a..0000000 --- a/src/LogLib/Objects/FileLogging/FileHandle.php +++ /dev/null @@ -1,99 +0,0 @@ -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; - } - } \ No newline at end of file diff --git a/src/LogLib/Objects/Options.php b/src/LogLib/Objects/Options.php index 7451f96..3e20eae 100644 --- a/src/LogLib/Objects/Options.php +++ b/src/LogLib/Objects/Options.php @@ -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; - } - } \ No newline at end of file diff --git a/src/LogLib/Objects/RuntimeOptions.php b/src/LogLib/Objects/RuntimeOptions.php index ced3eff..7f6fe4a 100644 --- a/src/LogLib/Objects/RuntimeOptions.php +++ b/src/LogLib/Objects/RuntimeOptions.php @@ -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; - } } \ No newline at end of file diff --git a/tests/exception_handling.php b/tests/exception_handling.php new file mode 100644 index 0000000..2153b25 --- /dev/null +++ b/tests/exception_handling.php @@ -0,0 +1,21 @@ +enableWriteToPackageData(); Log::register($options); Log::debug('net.nosial.optslib', 'This is a debug message');