Download the PHP package code-distortion/clarity-context without Composer
On this page you can find all versions of the php package code-distortion/clarity-context. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download code-distortion/clarity-context
More information about code-distortion/clarity-context
Files in code-distortion/clarity-context
Package clarity-context
Short Description A Context Tracker package for Laravel
License MIT
Homepage https://github.com/code-distortion/clarity-context
Informations about the package clarity-context
Clarity Context - Understand Your Exceptions
code-distortion/clarity-context is a Context Tracker package for Laravel that gives you a birds-eye-view of what your code was doing when an exception occurs.
Add context to your code. e.g.
This information is collected so when an exception occurs, it can be used to show what your code was doing at the time. e.g.
Clarity Suite
Clarity Context is a part of the Clarity Suite, designed to let you manage exceptions more easily:
- Clarity Context - Understand Your Exceptions
- Clarity Logger - Useful Exception Logs
- Clarity Control - Handle Your Exceptions
Table of Contents
- Installation
- Config File
- Add Context to Your Code
- Exception Logging
Installation
Install the package via composer:
Config File
Use the following command if you would like to publish the config/code_distortion.clarity_context.php
config file.
It simply gives you the option to turn this package on or off.
Add Context to Your Code
Clarity Context lets you add context details throughout your code. It keeps track of what's currently in the call stack, ready for when an exception occurs. e.g.
You can add strings to explain what's currently happening in a sentence, or associative arrays to show specific details about what your code is currently working with.
Add context throughout your code in relevant places. Pick places that will give you the most insight when tracking down a problem. Add as many as you feel necessary.
You can pass multiple values at once:
Note: Don't add sensitive details that you don't want to be logged!
If you use trace identifiers to identify requests, you can add these as well. A good place to add them would be in a service provider or request middleware.
Exception Logging
To log your exceptions, install a package like Clarity Logger that's aware of Clarity Context. Follow its installation instructions to add logging to your project.
Clarity Logger will automatically include your context details alongside the details it normally logs. e.g.
⚙️ Click for more information.
## Logging Exceptions (Advanced) Clarity Context collects and manages the context details you've added to your code. When an exception occurs, it builds a `CodeDistortion\ClarityContext\Context` object that can be used by the code doing the logging. This `Context` object contains the details you added (that were present in the call stack at the time). If you'd like to handle the logging yourself, or are building a package to do so - this involves updating Laravel's [exception handler](https://laravel.com/docs/10.x/errors#the-exception-handler) `app/Exceptions/Handler.php` to use these `Context` values. This section explains how to use this `Context` class. ### Obtaining the Context Object Use `Clarity::getExceptionContext($e)` to access the `CodeDistortion\ClarityContext\Context` object built for that exception. Then you can choose how to log the exception based on what's inside the `Context` object. ### The Context Object The `Context` object includes a variety of details about the exception, including: - the call stack / stack trace (based on `$e->getTrace()`, but with the file/line numbers shifted by one frame, so they make more sense), - your context details, that were present in the call stack at the time the exception occurred, - references to the location where the exception was thrown and caught. #### Stack Trace / Call Stack, and Frames You can retrieve details about the call stack frames using `$context->getStackTrace()` or `$context->getCallStack()`. They contain objects representing each frame. `getStackTrace()` contains the frames in order from most recent to oldest. `getCallStack()` is the same, except ordered from oldest to newest. They also contain the following methods to help you find particular frames and meta information. They are iterable, allowing them to be looped through. You can retrieve the following details from the Frame objects inside: > ***Note:*** Some of the methods like `getFunction()`, `getClass()`, `getObject()` won't always return a value. It depends on the circumstance. See [PHP's debug_backtrace method](https://www.php.net/manual/en/function.debug-backtrace.php) for more details. #### Meta Objects There are 5 types of Meta objects: - `ContextMeta` - when the application called `Clarity::context(…)` to add context details, - `CallMeta` - when the Control package ran some code for the application (e.g. using `Control::run()`), - `LastApplicationFrameMeta` - the location of the last application (i.e. non-vendor) frame, - `ExceptionThrownMeta` - the location the exception was thrown, - `ExceptionCaughtMeta` - the location the exception was caught. You can retrieve the following details from the Meta objects: There are several ways of retrieving Meta objects: Each of these methods accepts a meta-class string, or several of them, which limit the result. e.g. #### MetaGroup Objects When reporting the exception details, it's useful to group the Meta objects together. `MetaGroup` objects provide a way of grouping the Meta objects in a logical way. The Meta objects within are related, i.e. in the same frame and on near-by lines. Each MetaGroup contains similar details to the `Frame` object. ### Context Objects Without an Exception You can generate a Context object arbitrarily, without needing an exception. The Context object returned will contain the current context details, like it normally would.Testing This Package
- Clone this package:
git clone https://github.com/code-distortion/clarity-context.git .
- Run
composer install
to install dependencies - Run the tests:
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
SemVer
This library uses SemVer 2.0.0 versioning. This means that changes to X
indicate a breaking change: 0.0.X
, 0.X.y
, X.y.z
. When this library changes to version 1.0.0, 2.0.0 and so forth, it doesn't indicate that it's necessarily a notable release, it simply indicates that the changes were breaking.
Treeware
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Contributing
Please see CONTRIBUTING for details.
Code of Conduct
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.