PHP code example of larasell-dev / chronicle

1. Go to this page and download the library: Download larasell-dev/chronicle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

larasell-dev / chronicle example snippets


return [
    'enabled' => env('CHRONICLE_ENABLED', true),
    'default' => env('CHRONICLE_CHANNEL', 'file'),
    'channels' => [
        'stack' => ['driver' => 'stack', 'channels' => ['file', 'posthog'], 'ignore_exceptions' => false],
        'file' => ['driver' => 'single', 'path' => storage_path('logs/chronicle.log'), 'formatter' => ChronicleFormatter::class, 'level' => 'info'],
        'posthog' => ['driver' => 'posthog', 'api_key' => env('POSTHOG_API_KEY'), 'host' => env('POSTHOG_HOST', PostHogHandler::HOST_DEFAULT), 'level' => PostHogHandler::LEVEL_DEFAULT],
    ],
    'sampler' => null,
    'redact' => [
        'password', 'passwd', 'secret', 'token', 'api_key', 'apikey',
        'authorization', 'cookie', 'credential', 'credentials', 'private_key',
        'signature', 'sig', 'card_number', 'cvc', 'cvv', 'iban',
    ],
    'redactor' => null,
    'log_jobs' => env('CHRONICLE_LOG_JOBS', false),
    'log_ip' => env('CHRONICLE_LOG_IP', true),
    'service' => env('CHRONICLE_SERVICE'),
    'release' => env('CHRONICLE_RELEASE'),
    'environment' => env('CHRONICLE_ENVIRONMENT'),
    'host' => env('CHRONICLE_HOST'),
    'fields' => env('CHRONICLE_FIELDS', []),
];

use Larasell\Chronicle\WideEvents\Requests\Middleware\LogRequests;

->withMiddleware(function (Middleware $middleware): void {
    $middleware->prepend(LogRequests::class);
})

use Larasell\Chronicle\Facades\Chronicle;

Chronicle::add('cart', ['items' => 3, 'total' => 8900]);

use Larasell\Chronicle\Facades\Chronicle;
use Larasell\Chronicle\WideEvents\Enums\Level;

Chronicle::record('inventory.low', ['sku' => 'SKU-42', 'stock' => 2], Level::Warning);

Chronicle::recordError($exception, ['order.id' => 7]);

use Larasell\Chronicle\Exceptions\StructuredRuntimeException;

throw new StructuredRuntimeException(
    errorCode: 'billing.card-declined',
    message: 'Payment failed: card declined',
    why: 'The issuer declined the charge',
    fix: 'Try a different card',
);

use Larasell\Chronicle\Exceptions\HasStructuredException;
use Larasell\Chronicle\Exceptions\StructuredException;

final class PaymentDeclined extends Exception implements StructuredException
{
    use HasStructuredException;

    public static function byIssuer(): self
    {
        return self::structured(
            errorCode: 'billing.card-declined',
            message: 'Payment failed: card declined',
            fix: 'Try a different card',
        );
    }
}

use Larasell\Chronicle\Facades\Chronicle;

it('records the checkout', function () {
    $chronicle = Chronicle::fake();

    $this->post('/checkout');

    $chronicle->assertRecorded('request', fn (array $event) => $event['cart.items'] === 3);
});
bash
php artisan vendor:publish --tag="chronicle.config"