Skip to content

Commit 6fa129a

Browse files
committed
Replaced torann/geoip by stevebauman/location for optional GeoIP support
Improved username detection in `Formatter` headers
1 parent 8243527 commit 6fa129a

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- Drop Laravel 9 support, require Laravel v10.15 or higher for the new `DB::getRawQueryLog()` support.
1010
- PHP code style fixes by `laravel/pint` v1.10, now using more strict style rules (`laravel` preset).
1111
- Refactored whole codebase from `DB::getQueryLog()` to use the new `DB::getRawQueryLog()` method, so `ReplacesBindings` is no longer needed.
12+
- Replaced [torann/geoip](https://github.com/Torann/laravel-geoip) by [stevebauman/location](https://github.com/stevebauman/location) for optional GeoIP support.
13+
- Improved username detection in `Formatter` headers, so that it works both with default `email` field or custom `username()` method on `User` model.
1214

1315
## [1.0.1 (2023-02-26)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.0...v1.0.1)
1416

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ It reports a lot of metadata like total query count, total execution time, origi
6868

6969
## Optional
7070

71-
For optional GeoIP support (adding country information to client IP in log headers), you may install [torann/geoip](https://github.com/Torann/laravel-geoip) in your project:
71+
For optional GeoIP support (adding country information to client IP in log headers), you may install [stevebauman/location](https://github.com/stevebauman/location) in your project:
7272

7373
```bash
74-
$ composer require torann/geoip
75-
$ php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider"
74+
$ composer require stevebauman/location
75+
$ php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
7676
```
7777

78-
It will be auto-detected, no configuration needed for this.
78+
It will be auto-detected, no configuration needed for this. If you wish to use a different driver than the default [IpApi](https://ip-api.com/), e.g. `MaxMind` make sure you correctly configure it according to the docs: [Available Drivers](https://github.com/stevebauman/location#available-drivers)
7979

8080
## Development
8181

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"url": "https://github.com/mnabialek/laravel-version"
4848
},
4949
"suggest": {
50-
"torann/geoip": "GeoIP country detection for log headers information",
50+
"stevebaumann/location": "GeoIP country detection for log headers information",
5151
"symfony/thanks": "Star packages you use running 'composer thanks' command"
5252
}
5353
}

src/Formatter.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public function getHeader(): string
4444
return '';
4545
}
4646

47+
// Try to resolve username, which is stored in 'email' field by default, but could be mapped by username() method
48+
// by some custom trait.
49+
$username = ($user = Auth::user())
50+
? (method_exists($user, 'username') ? $user->username() : $user->email)
51+
: '';
52+
4753
$queryLog = DB::getRawQueryLog();
4854
$times = Arr::pluck($queryLog, 'time');
4955
$totalTime = $this->time(array_sum($times));
@@ -54,7 +60,7 @@ public function getHeader(): string
5460
'datetime' => Carbon::now()->toDateTimeString(),
5561
'origin' => $this->originLine(),
5662
'status' => sprintf('Executed %s queries in %s', count($queryLog), $totalTime),
57-
'user' => Auth::user()?->username(),
63+
'user' => $username,
5864
'env' => $this->app->environment(),
5965
'agent' => Request::userAgent() ?? PHP_SAPI,
6066
'ip' => $ip,
@@ -63,10 +69,10 @@ public function getHeader(): string
6369
];
6470
$headers = Arr::only($data, $headerFields);
6571

66-
// (optional) GeoIP lookup if torann/geoip is installed, appending country information to IP
67-
if (in_array('ip', $headerFields) && $ip !== '127.0.0.1' && function_exists('geoip')) {
68-
$geoip = geoip($ip);
69-
$headers['ip'] .= sprintf(' (%s / %s)', $geoip->iso_code, $geoip->country);
72+
// (optional) GeoIP lookup if stevebaumann/location is installed, appending country information to IP
73+
if (in_array('ip', $headerFields) && $ip !== '127.0.0.1' && class_exists('Stevebauman\Location\Facades\Location')) {
74+
$position = \Stevebauman\Location\Facades\Location::get();
75+
$headers['ip'] .= sprintf(' (%s / %s)', $position->isoCode, $position->countryName);
7076
}
7177

7278
// get formatted header lines with padded keys

0 commit comments

Comments
 (0)