Skip to content

Commit

Permalink
Processor for exceptions in context
Browse files Browse the repository at this point in the history
  • Loading branch information
itelmenko committed May 5, 2019
1 parent 7dbc532 commit 0abec86
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/Logger/Laravel/Logging/MySQLLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Logger\Laravel\Logging;

use Exception;
use Logger\Monolog\Handler\ExceptionsProcessor;
use Monolog\Logger;
use Logger\Monolog\Handler\MysqlHandler;

Expand All @@ -18,7 +19,9 @@ public function __invoke(array $config)
{
$channel = $config['name'] ?? env('APP_ENV');
$monolog = new Logger($channel);
$monolog->pushHandler(new MysqlHandler());
$handler = new MysqlHandler();
$handler->pushProcessor(new ExceptionsProcessor);
$monolog->pushHandler($handler);
return $monolog;
}
}
10 changes: 10 additions & 0 deletions src/Logger/Laravel/Models/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public function __construct(array $attributes = array())

parent::__construct($attributes);
}

public function changeConnection(string $db_connection)
{
$this->connection = $db_connection;
}

public function changeTable(string $table_name)
{
$this->table = $table_name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function (Blueprint $table) {
'ALERT',
'EMERGENCY'
])->default('INFO');
$table->longText('message');
$table->text('context');
$table->text('message');
$table->mediumText('context');
$table->timestamps();
}
);
Expand Down
16 changes: 16 additions & 0 deletions src/Logger/Monolog/Handler/ExceptionsProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Logger\Monolog\Handler;

class ExceptionsProcessor {

public function __invoke($record) {
if(!empty($record['context']['exception'])) {
$e = $record['context']['exception'];
if($e instanceof \Exception) {
$record['context']['trace'] = $e->getTrace();
}
}
return $record;
}
}
3 changes: 1 addition & 2 deletions src/Logger/Monolog/Handler/MysqlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class MysqlHandler extends AbstractProcessingHandler
{

protected function write(array $record)
{
Log::create([
Expand All @@ -21,4 +20,4 @@ protected function write(array $record)
'context' => $record['context']
]);
}
}
}

0 comments on commit 0abec86

Please sign in to comment.