Skip to content

Commit

Permalink
Eloquent model for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
itelmenko committed May 2, 2018
1 parent ad327a2 commit 7450983
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 35 deletions.
30 changes: 30 additions & 0 deletions src/Logger/Laravel/Models/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Logger\Models;

use Illuminate\Database\Eloquent\Model;


class Log extends Model {

protected $fillable = [
'instance',
'env',
'message',
'level',
'context'
];

protected $casts = [
'context' => 'array',
'extra' => 'array'
];

public function __construct(array $attributes = array())
{
$this->table = env('DB_LOG_TABLE', 'logs');
$this->connection = env('DB_LOG_CONNECTION', env('DB_CONNECTION', 'mysql'));

parent::__construct($attributes);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Logger\Laravel\Provider;
namespace Logger\Laravel\Providers;

use Illuminate\Support\ServiceProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ public function up()
env('DB_LOG_TABLE'),
function (Blueprint $table) {
$table->engine = 'InnoDB';

$table->bigIncrements('id');
$table->string('instance')->index();
$table->string('channel')->index();
$table->string('level')->index();
$table->string('level_name');
$table->text('message');
$table->string('env')->index();
$table->enum('level', [
'DEBUG',
'INFO',
'NOTICE',
'WARNING',
'ERROR',
'CRITICAL',
'ALERT',
'EMERGENCY'
])->default('INFO');
$table->longText('message');
$table->text('context');

$table->integer('remote_addr')->nullable()->unsigned();
$table->string('user_agent')->nullable();
$table->integer('created_by')->nullable()->index();

$table->dateTime('created_at');
$table->timestamps();
}
);
}
Expand Down
30 changes: 7 additions & 23 deletions src/Logger/Monolog/Handler/MysqlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,19 @@
use Illuminate\Support\Facades\Auth;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
use Logger\Models\Log;

class MysqlHandler extends AbstractProcessingHandler
{
protected $table;
protected $connection;

public function __construct($level = Logger::DEBUG, $bubble = true)
{
$this->table = env('DB_LOG_TABLE', 'logs');
$this->connection = env('DB_LOG_CONNECTION', env('DB_CONNECTION', 'mysql'));

parent::__construct($level, $bubble);
}

protected function write(array $record)
{
$data = [
Log::create([
'instance' => gethostname(),
'message' => $record['message'],
'channel' => $record['channel'],
'level' => $record['level'],
'level_name' => $record['level_name'],
'context' => json_encode(array_map("utf8_encode", $record['context'] )),
'remote_addr' => isset($_SERVER['REMOTE_ADDR']) ? ip2long($_SERVER['REMOTE_ADDR']) : null,
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
'created_by' => Auth::id() > 0 ? Auth::id() : null,
'created_at' => $record['datetime']->format('Y-m-d H:i:s')
];

DB::connection($this->connection)->table($this->table)->insert($data);
'env' => $record['channel'],
'message' => $record['message'],
'level' => $record['level_name'],
'context' => $record['context'] // 'context' => json_encode(array_map("utf8_encode", $record['context'] )),
]);
}
}

0 comments on commit 7450983

Please sign in to comment.