Skip to content

Commit

Permalink
Merge pull request #254 from stats4sd/add-database-log
Browse files Browse the repository at this point in the history
Add Database Log
  • Loading branch information
dan-tang-ssd authored Mar 12, 2024
2 parents 82a88ec + 6e7148c commit b1daefb
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ ADMIN_PASSWORD=password

# Laravel Flare
FLARE_KEY=

# Database log level
DB_LOG_LEVEL=ERROR
6 changes: 5 additions & 1 deletion app/Console/Commands/GetExchangeRatesForOneDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use Carbon\Carbon;
use App\Services\DBLog;
use App\Models\Currency;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -30,6 +31,8 @@ class GetExchangeRatesForOneDay extends Command
*/
public function handle()
{
DBLog::info('GetExchangeRatesForOneDay', 'SYSTEM', 'start');

// get yesterday date
$date = Carbon::now()->subDays(1)->toDateString();

Expand All @@ -41,10 +44,11 @@ public function handle()
foreach ($currencies as $currency) {
GetOneDayExchangeRates::dispatch($currency, $date);
$this->comment('dispatched job for ' . $currency . ' and the date ' . $date);
DBLog::debug('GetExchangeRatesForOneDay', 'SYSTEM', 'dispatched job for ' . $currency . ' and the date ' . $date);
}

$this->info('done!');

DBLog::info('GetExchangeRatesForOneDay', 'SYSTEM', 'end');
}

}
27 changes: 18 additions & 9 deletions app/Jobs/GetOneDayExchangeRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

namespace App\Jobs;

use Carbon\Carbon;
use App\Services\DBLog;
use App\Models\Currency;
use App\Models\ExchangeRate;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\RateLimited;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Queue\Middleware\ThrottlesExceptions;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class GetOneDayExchangeRates implements ShouldQueue
{
Expand All @@ -28,11 +29,14 @@ class GetOneDayExchangeRates implements ShouldQueue
public function __construct(public Currency $currency, public string $date)
{
//
DBLog::info('GetOneDayExchangeRates', 'SYSTEM', 'construct() start');
}

// what middleware should the job pass through?
public function middleware(): array
{
DBLog::info('GetOneDayExchangeRates', 'SYSTEM', 'middleware() start');

return [
new RateLimited('exchange_rates'),
//new ThrottlesExceptions(1, 1),
Expand All @@ -41,6 +45,8 @@ public function middleware(): array

public function retryUntil(): \DateTime
{
DBLog::info('GetOneDayExchangeRates', 'SYSTEM', 'retryUntil() start, add 10 minutes');

return now()->addMinutes(10);
}

Expand All @@ -50,6 +56,8 @@ public function retryUntil(): \DateTime
*/
public function handle(): void
{
DBLog::info('GetOneDayExchangeRates', 'SYSTEM', 'handle() start');

$startDate = $this->date;
$endDate = $this->date;

Expand All @@ -64,6 +72,7 @@ public function handle(): void
])
->throw(function (Response $response, RequestException $exception) {
Log::error($exception->getMessage());
DBLog::error('GetOneDayExchangeRates', 'SYSTEM', $exception->getMessage());
})
->json();

Expand All @@ -80,12 +89,12 @@ public function handle(): void
'date' => $date,
'rate' => $value,
];

}
}

$this->currency->exchangeRates()->createMany($rates);
DBLog::debug('GetOneDayExchangeRates', 'SYSTEM', 'create ' . count($rates) . ' exchange rate records');

DBLog::info('GetOneDayExchangeRates', 'SYSTEM', 'handle() end');
}

}
14 changes: 14 additions & 0 deletions app/Models/SystemLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;

class SystemLog extends Model
{
use CrudTrait;

protected $table = 'system_logs';
protected $guarded = ['id'];
}
52 changes: 52 additions & 0 deletions app/Services/DBLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Services;

use App\Models\SystemLog;
use Illuminate\Support\Str;

class DBLog
{
public static $logLevels = array('FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG');

public static function log(string $level, string $program, string $createdBy, string $message)
{
$systemLogLevel = Str::upper(env('DB_LOG_LEVEL', 'ERROR'));
$systemLogLevelNumber = array_search($systemLogLevel, DBLog::$logLevels);
$logLevelNumber = array_search($level, DBLog::$logLevels);

if ($logLevelNumber <= $systemLogLevelNumber) {
SystemLog::create([
'level' => $level,
'program' => $program,
'created_by' => $createdBy,
'message' => $message,
]);
}
}

public static function fatal(string $program, string $createdBy, string $message)
{
DBLog::log('FATAL', $program, $createdBy, $message);
}

public static function error(string $program, string $createdBy, string $message)
{
DBLog::log('ERROR', $program, $createdBy, $message);
}

public static function warn(string $program, string $createdBy, string $message)
{
DBLog::log('WARN', $program, $createdBy, $message);
}

public static function info(string $program, string $createdBy, string $message)
{
DBLog::log('INFO', $program, $createdBy, $message);
}

public static function debug(string $program, string $createdBy, string $message)
{
DBLog::log('DEBUG', $program, $createdBy, $message);
}
}
31 changes: 31 additions & 0 deletions database/migrations/2024_03_11_104041_create_system_logs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('system_logs', function (Blueprint $table) {
$table->id();
$table->string('level')->index();
$table->string('program')->index();
$table->string('message');
$table->string('created_by');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('system_logs');
}
};

0 comments on commit b1daefb

Please sign in to comment.