diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-16 12:06:18 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-16 12:06:18 +0200 |
commit | 0734807eef5a6b638b06d57aef6ee59ac78a3456 (patch) | |
tree | 3af73fdc293ab18962078c1a92e07d803009f1f5 /src | |
parent | d0abc27c079f058c40a74b89e54bd21ba4aad53e (diff) | |
parent | ac48332166ce28fcb1a2fc130c7f5adbc760e42d (diff) |
Merge remote-tracking branch 'MyIgel/logentry-model'
Diffstat (limited to 'src')
-rw-r--r-- | src/Logger/EngelsystemLogger.php | 13 | ||||
-rw-r--r-- | src/Models/BaseModel.php | 23 | ||||
-rw-r--r-- | src/Models/LogEntry.php | 46 |
3 files changed, 80 insertions, 2 deletions
diff --git a/src/Logger/EngelsystemLogger.php b/src/Logger/EngelsystemLogger.php index 1f255b69..aab8026a 100644 --- a/src/Logger/EngelsystemLogger.php +++ b/src/Logger/EngelsystemLogger.php @@ -2,6 +2,7 @@ namespace Engelsystem\Logger; +use Engelsystem\Models\LogEntry; use Psr\Log\AbstractLogger; use Psr\Log\InvalidArgumentException; use Psr\Log\LogLevel; @@ -19,6 +20,14 @@ class EngelsystemLogger extends AbstractLogger LogLevel::WARNING, ]; + /** @var LogEntry */ + protected $log; + + public function __construct(LogEntry $log) + { + $this->log = $log; + } + /** * Logs with an arbitrary level. * @@ -33,12 +42,12 @@ class EngelsystemLogger extends AbstractLogger public function log($level, $message, array $context = []) { if (!$this->checkLevel($level)) { - throw new InvalidArgumentException(); + throw new InvalidArgumentException('Unknown log level: ' . $level); } $message = $this->interpolate($message, $context); - LogEntry_create($level, $message); + $this->log->create(['level' => $level, 'message' => $message]); } /** diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php new file mode 100644 index 00000000..cf718e4f --- /dev/null +++ b/src/Models/BaseModel.php @@ -0,0 +1,23 @@ +<?php + +namespace Engelsystem\Models; + +use Illuminate\Database\Eloquent\Model; + +abstract class BaseModel extends Model +{ + /** @var bool Disable timestamps by default because of "Datensparsamkeit" */ + public $timestamps = false; + + /** + * @param array $attributes + * @return BaseModel + */ + public function create(array $attributes = []) + { + $instance = new static($attributes); + $instance->save(); + + return $instance; + } +} diff --git a/src/Models/LogEntry.php b/src/Models/LogEntry.php new file mode 100644 index 00000000..ca9702de --- /dev/null +++ b/src/Models/LogEntry.php @@ -0,0 +1,46 @@ +<?php + +namespace Engelsystem\Models; + +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; + +class LogEntry extends BaseModel +{ + /** @var bool enable timestamps for created_at */ + public $timestamps = true; + + /** @var null Disable updated_at */ + const UPDATED_AT = null; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'level', + 'message', + ]; + + /** + * @param $keyword + * @return Builder[]|Collection|LogEntry[] + */ + public static function filter($keyword = null) + { + $query = LogEntry::query() + ->select() + ->orderByDesc('created_at') + ->orderByDesc('id') + ->limit(10000); + + if (!empty($keyword)) { + $query + ->where('level', '=', $keyword) + ->orWhere('message', 'LIKE', '%' . $keyword . '%'); + } + + return $query->get(); + } +} |