Skip to content

Commit 179a2de

Browse files
committed
Fixed issue around php artisan optimize - when there’s an error with any of the packages, the Auth and DB classes are not available during composer process
1 parent 0feb3b5 commit 179a2de

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Logger/Monolog/Handler/MysqlHandler.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Logger\Monolog\Handler;
44

55
use DB;
6+
use Illuminate\Support\Facades\Auth;
67
use Monolog\Logger;
78
use Monolog\Handler\AbstractProcessingHandler;
89

@@ -21,6 +22,12 @@ public function __construct($level = Logger::DEBUG, $bubble = true)
2122

2223
protected function write(array $record)
2324
{
25+
$created_by = null;
26+
27+
if (method_exists('Auth', 'id')) {
28+
$created_by = Auth::id() > 0 ? Auth::id() : null;
29+
}
30+
2431
$message = explode(': ', $record['message'], 2);
2532

2633
$body = isset($message[1]) ? $message[1] : $message[0];
@@ -36,10 +43,12 @@ protected function write(array $record)
3643
'context' => json_encode($record['context']),
3744
'remote_addr' => isset($_SERVER['REMOTE_ADDR']) ? ip2long($_SERVER['REMOTE_ADDR']) : null,
3845
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
39-
'created_by' => \Auth::id() > 0 ? \Auth::id() : null,
46+
'created_by' => $created_by,
4047
'created_at' => $record['datetime']->format('Y-m-d H:i:s')
4148
];
4249

43-
DB::connection($this->connection)->table($this->table)->insert($data);
50+
if (method_exists('DB', 'connection')) {
51+
DB::connection($this->connection)->table($this->table)->insert($data);
52+
}
4453
}
4554
}

0 commit comments

Comments
 (0)