Skip to content

Commit

Permalink
Make monolog/monolog dependency really optional
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Jan 10, 2025
1 parent 55b770a commit 0aac67f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"php": "^8.1",
"illuminate/cache": "^10.0 || ^11.0",
"illuminate/console": "^10.0 || ^11.0",
"illuminate/support": "^10.0 || ^11.0",
"monolog/monolog": "^3.0"
"illuminate/support": "^10.0 || ^11.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.50",
Expand Down
2 changes: 2 additions & 0 deletions config/geoip.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
| Here you may configure the log settings for when a location is not found
| for the IP provided.
|
| Requires Monolog to be installed: `composer install monolog/monolog`
|
*/

'log_failures' => true,
Expand Down
14 changes: 10 additions & 4 deletions src/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace InteractionDesignFoundation\GeoIP;

use Monolog\Logger;
use Illuminate\Support\Arr;
use Illuminate\Cache\CacheManager;
use Monolog\Handler\StreamHandler;

/**
* @psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
Expand Down Expand Up @@ -148,8 +146,16 @@ private function find($ip = null): Location
return $location;
} catch (\Exception $e) {
if ($this->config('log_failures', true) === true) {
$log = new Logger('geoip');
$log->pushHandler(new StreamHandler(storage_path('logs/geoip.log'), Logger::ERROR));
if (! class_exists(\Monolog\Logger::class)) {
throw new \RuntimeException(
'monolog/monolog composer package is not installed, but required with the enabled geoip.log_failures config option.',
0,
$e
);
}

$log = new \Monolog\Logger('geoip');
$log->pushHandler(new \Monolog\Handler\StreamHandler(storage_path('logs/geoip.log'), \Monolog\Logger::ERROR));

Check failure on line 158 in src/GeoIP.php

View workflow job for this annotation

GitHub Actions / Psalm

DeprecatedConstant

src/GeoIP.php:158:106: DeprecatedConstant: Constant Monolog\Logger::ERROR is deprecated (see https://psalm.dev/170)
$log->error($e);
}
}
Expand Down

0 comments on commit 0aac67f

Please sign in to comment.