Skip to content

Commit

Permalink
Made datetime format of formatted header configurable and added TZ of…
Browse files Browse the repository at this point in the history
…fset
  • Loading branch information
onlime committed Mar 24, 2024
1 parent c4d914a commit 3932b18
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

- Made datetime format of formatted header configurable and added TZ offset to default format `Y-m-d H:i:s P`.
- ...

## [v1.2.2 (2024-03-21)](https://github.com/onlime/laravel-sql-reporter/compare/v1.2.1...v1.2.2)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ It reports a lot of metadata like total query count, total execution time, origi
SQL_REPORTER_QUERIES_REPORT_PATTERN='/^(?!select\s|start transaction|commit|(insert into|update|delete from) `(sessions|jobs|bans|logins)`).*/i'
SQL_REPORTER_QUERIES_MIN_EXEC_TIME=0
SQL_REPORTER_QUERIES_FILE_NAME="[Y-m]-log"
SQL_REPORTER_FORMAT_HEADER_FIELDS="origin,datetime,status,user,env,agent,ip,host,referer"
SQL_REPORTER_FORMAT_HEADER_FIELDS="datetime,origin,status,user,env,agent,ip,host,referer"
SQL_REPORTER_FORMAT_ENTRY_FORMAT="-- Query [query_nr] [[query_time]]\\n[query]"
```

Expand Down
9 changes: 7 additions & 2 deletions config/sql-reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
/*
* Header fields, comma-separated. Available options:
*
* - origin: where this query coming from - method/request or artisan command
* - datetime: date and time when the first query was executed
* - origin: where this query coming from - method/request or artisan command
* - status: total query count and execution time for all queries
* - user: username of authenticated user
* - guard: name of the auth guard (defaults to 'web' if not logged in)
Expand All @@ -44,9 +44,14 @@
* data if origin is an Artisan command.
*/
'header_fields' => array_filter(explode(',', env('SQL_REPORTER_FORMAT_HEADER_FIELDS',
'origin,datetime,status,user,guard,env,agent,ip,host,referer'
'datetime,origin,status,user,guard,env,agent,ip,host,referer'
))),

/*
* Header datetime format.
*/
'header_datetime_format' => env('SQL_REPORTER_FORMAT_HEADER_DATETIME_FORMAT', 'Y-m-d H:i:s P'),

/*
* Single entry format. Available options:
*
Expand Down
8 changes: 8 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public function headerFields(): array
return $this->repository->get('sql-reporter.formatting.header_fields');
}

/**
* Get header datetime format.
*/
public function headerDatetimeFormat(): string
{
return $this->repository->get('sql-reporter.formatting.header_datetime_format');
}

/**
* Get query format that should be used to save query.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getHeader(): string

// TODO: datetime information should be replaced by lowest query timestamp, see https://github.com/laravel/framework/pull/37514
$data = [
'datetime' => Carbon::now()->toDateTimeString(),
'datetime' => Carbon::now()->format($this->config->headerDatetimeFormat()),
'origin' => $this->originLine(),
'status' => sprintf('Executed %s queries in %s', count($queryLog), $totalTime),
'user' => $username,
Expand Down
10 changes: 6 additions & 4 deletions tests/Feature/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
$app->shouldReceive('environment')->once()->withNoArgs()->andReturn('testing');
$config->shouldReceive('useSeconds')->once()->withNoArgs()->andReturn(false);
$config->shouldReceive('headerFields')->once()->withNoArgs()
->andReturn(explode(',', 'origin,datetime,status,user,env,agent,ip,host,referer'));
->andReturn(explode(',', 'datetime,origin,status,user,env,agent,ip,host,referer'));
$config->shouldReceive('headerDatetimeFormat')->once()->withNoArgs()->andReturn('Y-m-d H:i:s P');
$request = Mockery::mock(Request::class);
$app->shouldReceive('offsetGet')->times(3)->with('request')->andReturn($request);
$request->shouldReceive('method')->once()->withNoArgs()->andReturn('DELETE');
Expand All @@ -26,7 +27,7 @@
$request->shouldReceive('userAgent')->once()->withNoArgs()->andReturn('Mozilla/5.0');
$request->shouldReceive('header')->once()->with('referer')->andReturn('');

$now = '2015-03-04 08:12:07';
$now = '2015-03-04 08:12:07 +00:00';
Carbon::setTestNow($now);

DB::shouldReceive('getRawQueryLog')->once()->withNoArgs()->andReturn([
Expand Down Expand Up @@ -63,15 +64,16 @@
$app->shouldReceive('environment')->once()->withNoArgs()->andReturn('testing');
$config->shouldReceive('useSeconds')->once()->withNoArgs()->andReturn(false);
$config->shouldReceive('headerFields')->once()->withNoArgs()
->andReturn(explode(',', 'origin,datetime,status,user,env,agent,ip,host,referer'));
->andReturn(explode(',', 'datetime,origin,status,user,env,agent,ip,host,referer'));
$config->shouldReceive('headerDatetimeFormat')->once()->withNoArgs()->andReturn('Y-m-d H:i:s P');
$request = Mockery::mock(Request::class);
$app->shouldReceive('offsetGet')->twice()->with('request')->andReturn($request);
$request->shouldReceive('server')->once()->with('argv', [])->andReturn('php artisan test');
$request->shouldReceive('ip')->once()->withNoArgs()->andReturn('127.0.0.1');
$request->shouldReceive('userAgent')->once()->withNoArgs()->andReturn('Mozilla/5.0');
$request->shouldReceive('header')->once()->with('referer')->andReturn('');

$now = '2015-03-04 08:12:07';
$now = '2015-03-04 08:12:07 +00:00';
Carbon::setTestNow($now);

DB::shouldReceive('getRawQueryLog')->once()->withNoArgs()->andReturn([]);
Expand Down

0 comments on commit 3932b18

Please sign in to comment.