geocoder-php / GeocoderLaravel

Geocoder service provider for Laravel
http://geocoder-php.org/GeocoderLaravel/
MIT License
701 stars 102 forks source link

Inject Logger configuration #145

Closed simonschaufi closed 4 years ago

simonschaufi commented 5 years ago

General Information

GeocoderLaravel Version: any Laravel Version: 5.6 PHP Version: any Operating System and Version: any

Issue Description

I would like to log Exceptions during geocoding but don't know how to configure my logger. I already asked how to use the logger in Geocoder package and got an answer: https://github.com/geocoder-php/Geocoder/issues/901#issuecomment-440984616. What is missing is any kind of configuration to use the Laravel Logger

Steps to Replicate

  1. Do a geocoding which will throw an exception (for example invalid API key)
  2. In many places the internal logger interface is called like $this->log()
  3. Write this log message to a file
eugenekurasov commented 5 years ago

Hi, Laravel not supported automatic setters.

You can create PR and insert Logger to ProviderAndDumperAggregator https://github.com/geocoder-php/GeocoderLaravel/blob/11408306ec9b148a7f465678488426f13dc26f25/src/ProviderAndDumperAggregator.php#L138 Here is example like can do this (I didn't testing this solution):

$this->aggregator->registerProvider($provider);
if (in_array('Psr\Log\LoggerAwareTrait', class_uses($provider) && app('Illuminate\Log\Logger') !== null) {
   $provider->setLogger(app('Illuminate\Log\Logger'))
}

I hope this help you.

simonschaufi commented 5 years ago

I tried debugging the whole process and this method got never called.

This is my config:

    'providers' => [
        Chain::class => [
            GoogleMaps::class => [
                env('GOOGLEMAPS_LOCALE', 'de-DE'),
                env('GOOGLEMAPS_APIKEY_SERVER'),
            ],
            Nominatim::class => [
                'https://nominatim.openstreetmap.org',
                'info@example.com',
            ],
        ],
    ],

and in my App I call:

$query = '<The address to be geocoded>';
$geoCoder = app('geocoder');
$addresses = $geoCoder->geocode($query)->get();

I would highly appreciate it if you could send me a working solution with the logger running.