-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from stats4sd/add-database-log
Add Database Log
- Loading branch information
Showing
6 changed files
with
123 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,6 @@ ADMIN_PASSWORD=password | |
|
||
# Laravel Flare | ||
FLARE_KEY= | ||
|
||
# Database log level | ||
DB_LOG_LEVEL=ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
database/migrations/2024_03_11_104041_create_system_logs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |