diff --git a/LICENSE b/LICENSE index 45c311c..41efa40 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2025 Gent Uka +Copyright (c) 2025 schauinsland-reisen gmbh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 10b500e..cdf28b5 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ * [Example .env](#example-env) * [Usage](#usage) * [Output Example](#output-example) + * [Publish Configuration (Optional)](#publish-configuration-optional) * [Bug report or Feature request](#bug-report-or-feature-request) * [Want to Contribute?](#want-to-contribute) * [Code of Conduct](#code-of-conduct) @@ -33,6 +34,7 @@ The library automatically merges with Laravel's `config/logging.php`, eliminatin - **Captures All Laravel Errors in Debug Mode**: Automatically logs all Laravel exceptions and errors to Splunk when the application is in debug mode, providing comprehensive error insights during development. +Additionally, it is possible and optional to merge the configuration manually if needed. For more details, see [Publish Configuration (Optional)](#publish-configuration-optional). ## Installation @@ -116,6 +118,24 @@ For more custom logging options, refer to the [Laravel Logging Documentation](ht } ``` +## Publish Configuration (Optional) + +By default, the configuration is automatically merged into logging.php. However, you can still publish the configuration file to the config directory if needed. To publish the configuration, run: +```bash +php artisan vendor:publish --provider=Schauinsland\\SplunkLogger\\ServiceProvider +``` + +After publishing, update your `logging.php` configuration file to include the published settings: +```php +return [ + 'channels' => [ + 'splunk' => (require config_path('laravel-splunk-logger.php'))['splunk'], + ], + // Other channels... +]; +``` + + ## Bug report or Feature request If you encounter a bug or have a feature request, please [create an issue](https://github.com/schauinsland/laravel-splunk-logger/issues). diff --git a/composer.json b/composer.json index 99e0e02..1c6c02d 100644 --- a/composer.json +++ b/composer.json @@ -21,5 +21,8 @@ "Schauinsland\\SplunkLogger\\ServiceProvider" ] } - } + }, + "require": { + "ext-json": "*" + } } diff --git a/src/Configuration.php b/src/Configuration.php index 3659858..431d4da 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -4,5 +4,10 @@ 'splunk' => [ 'driver' => 'custom', 'via' => \Schauinsland\SplunkLogger\SplunkLogger::class, + 'SPLUNK_URL' => env('SPLUNK_URL'), + 'SPLUNK_TOKEN' => env('SPLUNK_TOKEN'), + 'SPLUNK_INDEX' => env('SPLUNK_INDEX'), + 'SPLUNK_SOURCE' => env('SPLUNK_SOURCE', 'schauinsland/laravel-splunk-logger'), + 'SPLUNK_SSL_VERIFY' => env('SPLUNK_SSL_VERIFY', true), ] ]; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 41b8b7e..c898e0b 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -6,18 +6,26 @@ class ServiceProvider extends BaseServiceProvider { - private string $configPath = '/Configuration.php'; + private string $configPath = '/Configuration.php'; - public function boot(): void - { - $this->loadSplunkConfig(); - } + public function boot(): void + { + $this->publishes( + [__DIR__ . $this->configPath => config_path('laravel-splunk-logger.php')], + ); - private function loadSplunkConfig(): void - { - $this->mergeConfigFrom( - __DIR__ . $this->configPath, - 'logging.channels' - ); - } + $this->mergeSplunkConfig(); + } + + private function mergeSplunkConfig(): void + { + if (config('laravel-splunk-logger')) { + return; + } + + $this->mergeConfigFrom( + __DIR__ . $this->configPath, + 'logging.channels' + ); + } } diff --git a/src/SplunkLoggerHandler.php b/src/SplunkLoggerHandler.php index 059c8bd..7819e07 100644 --- a/src/SplunkLoggerHandler.php +++ b/src/SplunkLoggerHandler.php @@ -2,6 +2,7 @@ namespace Schauinsland\SplunkLogger; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; use Monolog\Handler\AbstractProcessingHandler; use Monolog\LogRecord; @@ -10,17 +11,20 @@ class SplunkLoggerHandler extends AbstractProcessingHandler { public function write(LogRecord $record): void { + $splunkConfiguration = Config::get('logging.channels.splunk'); + $payload = [ 'event' => json_encode($record->toArray()), - 'source' => env('SPLUNK_SOURCE'), - 'index' => env('SPLUNK_INDEX'), + 'source' => $splunkConfiguration['SPLUNK_SOURCE'], + 'index' => $splunkConfiguration['SPLUNK_INDEX'], ]; Http::withHeaders([ - 'Authorization' => 'Splunk ' . env('SPLUNK_TOKEN'), - ]) - ->withOptions([ - 'verify' => env('SPLUNK_VERIFY', false), - ])->post(env('SPLUNK_URL'), $payload); + 'Authorization' => 'Splunk ' . $splunkConfiguration['SPLUNK_TOKEN'], + ])->withOptions([ + 'verify' => $splunkConfiguration['SPLUNK_SSL_VERIFY'], + ])->post( + $splunkConfiguration['SPLUNK_URL'], $payload + ); } }