Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# CHANGELOG

## [1.0.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.1...main)
## [1.1.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.1.0...main)

- ...

## [1.1.0 (2023-07-16)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.1...v1.1.0)

- Drop Laravel 9 support, require Laravel v10.15 or higher for the new [`DB::getRawQueryLog()`](https://github.com/laravel/framework/pull/47507) support.
- PHP code style fixes by `laravel/pint` v1.10, now using more strict style rules (`laravel` preset).
- Refactored whole codebase from `DB::getQueryLog()` to use the new `DB::getRawQueryLog()` method, so `ReplacesBindings` is no longer needed.
- Replaced [torann/geoip](https://github.com/Torann/laravel-geoip) by [stevebauman/location](https://github.com/stevebauman/location) for optional GeoIP support.
- Improved username detection in `Formatter` headers, so that it works both with default `email` field or custom `username()` method on `User` model.

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

- Allow bindings to be null.
Expand Down
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
This module allows you to log SQL queries to log file in Laravel framework. It's useful mainly
when developing your application to verify whether your queries are valid and to make sure your application doesn't run too many or too slow database queries.

You may also use this in production as it should not cause a lot of overhead. Logged queries can be limited by query pattern, and logging only occurs at the end of each request or artisan command execution.
You may also use this in production as it should not cause a lot of overhead. Logged queries can be limited by query pattern, and logging only occurs at the end of each request or artisan command execution, not per query execution.

It reports a lot of metadata like total query count, total execution time, origin (request URL/console command), authenticated user, app environment, client browser agent / IP / hostname.

Expand All @@ -22,9 +22,9 @@ It reports a lot of metadata like total query count, total execution time, origi
in console to install this module (Notice `--dev` flag - it's recommended to use this package only for development).

Laravel uses package auto-discovery, and it will automatically load this service provider, so you don't need to add anything into the `providers` section of `config/app.php`.

2. Run the following in your console to publish the default configuration file:

```bash
$ php artisan vendor:publish --provider="Onlime\LaravelSqlReporter\Providers\ServiceProvider"
```
Expand Down Expand Up @@ -57,25 +57,25 @@ It reports a lot of metadata like total query count, total execution time, origi
SQL_REPORTER_QUERIES_INCLUDE_PATTERN="/^(?!SELECT).*/i"
SQL_REPORTER_QUERIES_EXCLUDE_PATTERN="/^UPDATE.*(last_visit|remember_token)/i"
```

If you have also `.env.example` it's recommended to add those entries also in `.env.example` file just to make sure everyone knows about those env variables. Be aware that `SQL_REPORTER_DIRECTORY` is directory inside storage directory.

To find out more about those setting please take a look at [Configuration file](config/sql-reporter.php)

4. Make sure directory specified in `.env` file exists in storage path, and you have valid permissions to create and modify files in this directory (If it does not exist this package will automatically create it when needed, but it's recommended to create it manually with valid file permissions)

5. Make sure on live server you will set logging SQL queries to false in your `.env` file: `SQL_REPORTER_QUERIES_ENABLED=false`. This package is recommended to be used only for development to not impact production application performance.

## Optional

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:
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:

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

It will be auto-detected, no configuration needed for this.
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)

## Development

Expand All @@ -100,7 +100,7 @@ This package was inspired by [mnabialek/laravel-sql-logger](https://github.com/m

- Query logging is not triggered upon each query execution but instead at a final step, using `RequestHandled` and `CommandFinished` events.
- This allows us to include much more information about the whole query executions like total query count, total execution time, and very detailed header information like origin (request URL/console command), authenticated user, app environment, client browser agent / IP / hostname.
- This package is greatly simplified and only provides support for Laravel 8+ / PHP 8
- This package is greatly simplified and only provides support for Laravel 10 / PHP 8.1+
- It uses the Laravel built-in query logging (`DB::enableQueryLog()`) which logs all queries in memory, which should perform much better than writing every single query to the log file.
- By default, `onlime/laravel-sql-reporter` produces much nicer log output, especially since we only write header information before the first query.

Expand Down Expand Up @@ -153,12 +153,11 @@ All changes are listed in [CHANGELOG](CHANGELOG.md)
- If your application crashes, this package will not log any queries, as logging is only triggered at the end. As alternative, you could use [mnabialek/laravel-sql-logger](https://github.com/mnabialek/laravel-sql-logger) which triggers sql logging on each query execution.
- It's currently not possible to log slow queries into a separate logfile. I wanted to keep that package simpel.

## Todo
## TODO

- [ ] Improve unit testing to reach 100% coverage
- [ ] Integrate Coveralls.io and add test coverage status badge to README
- [ ] Add browser type information to log headers, using hisorange/browser-detect
- [ ] Support for Lumen
- [ ] Add browser type information to log headers, maybe using [hisorange/browser-detect](https://github.com/hisorange/browser-detect)

## License

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
],
"require": {
"php": "^8.1",
"illuminate/support": "^9.0 || ^10.0",
"illuminate/filesystem": "^9.0 || ^10.0",
"illuminate/container": "^9.0 || ^10.0",
"illuminate/support": "^10.15.0",
"illuminate/filesystem": "^10.15.0",
"illuminate/container": "^10.15.0",
"nesbot/carbon": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.2",
"mockery/mockery": "^1.0",
"laravel/framework": "^9.0 || ^10.0",
"laravel/pint": "^1.5"
"laravel/framework": "^10.15.0",
"laravel/pint": "^1.10"
},
"autoload": {
"psr-4": {
Expand All @@ -47,7 +47,7 @@
"url": "https://github.com/mnabialek/laravel-version"
},
"suggest": {
"torann/geoip": "GeoIP country detection for log headers information",
"stevebaumann/location": "GeoIP country detection for log headers information",
"symfony/thanks": "Star packages you use running 'composer thanks' command"
}
}
Expand Down
36 changes: 17 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="./tests/_coverage" lowUpperBound="35" highLowerBound="70"/>
<text outputFile="php://stdout" showUncoveredFiles="true"/>
</report>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<coverage>
<report>
<html outputDirectory="./tests/_coverage" lowUpperBound="35" highLowerBound="70"/>
<text outputFile="php://stdout" showUncoveredFiles="true"/>
</report>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
5 changes: 0 additions & 5 deletions pint.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
{
"preset": "laravel",
"rules": {
"braces": false,
"blank_line_before_statement": false,
"class_definition": false,
"binary_operator_spaces": false,
"concat_space": false,
"no_unused_imports": false,
"phpdoc_separation": false,
"Laravel/laravel_phpdoc_alignment": false,
"modernize_strpos": true
},
"exclude": [
Expand Down
125 changes: 0 additions & 125 deletions src/Concerns/ReplacesBindings.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

class Config
{
/**
* Config constructor.
*/
public function __construct(
protected ConfigRepository $repository
) {
Expand Down
7 changes: 2 additions & 5 deletions src/FileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class FileName
{
/**
* FileName constructor.
*/
public function __construct(
private Container $app,
private Config $config
Expand All @@ -22,8 +19,8 @@ public function __construct(
public function getLogfile(): string
{
return
$this->parseFileName($this->config->queriesFileName()) .
$this->suffix() .
$this->parseFileName($this->config->queriesFileName()).
$this->suffix().
$this->config->fileExtension();
}

Expand Down
Loading