danielme85 / laravel-log-to-db

@@ -2,7 +2,6 @@
Loading
2 2
3 3
namespace danielme85\LaravelLogToDB\Jobs;
4 4
5 -
use danielme85\LaravelLogToDB\Models\CreateLogFromRecord;
6 5
use danielme85\LaravelLogToDB\Models\DBLogException;
7 6
use Illuminate\Bus\Queueable;
8 7
use Illuminate\Queue\InteractsWithQueue;

@@ -3,7 +3,10 @@
Loading
3 3
namespace danielme85\LaravelLogToDB;
4 4
5 5
use danielme85\LaravelLogToDB\Models\DBLogException;
6 +
use Monolog\Formatter\LineFormatter;
6 7
use Monolog\Handler\AbstractProcessingHandler;
8 +
use Monolog\Handler\ErrorLogHandler;
9 +
use Monolog\Logger;
7 10
8 11
/**
9 12
 * Class LogToDbHandler
@@ -54,22 +57,25 @@
Loading
54 57
     */
55 58
    protected function write(array $record): void
56 59
    {
57 -
        if (!empty($record)) {
58 -
            if (!empty($record['context']['exception']) && is_object($record['context']['exception']) &&
59 -
                get_class($record['context']['exception']) === DBLogException::class) {
60 -
                //Do nothing if empty log record or an error Exception from itself.
61 -
            } else {
62 -
                try {
63 -
                    $log = new LogToDB($this->config);
64 -
                    $log->newFromMonolog($record);
65 -
                } catch (DBLogException $e) {
66 -
                    //do nothing if exception of self
67 -
                } catch (\Throwable $e) {
68 -
                    //convert any runtime Exception while logging to a special class so we can avoid our own
69 -
                    //exceptions for 99% less infinite loops!
70 -
                    throw new DBLogException($e->getMessage());
71 -
                }
72 -
            }
60 +
        try {
61 +
            $log = new LogToDB($this->config);
62 +
            $log->newFromMonolog($record);
63 +
        } catch (\Exception $e) {
64 +
            $this->emergencyLog([
65 +
                'message' => 'There was an error while trying to write the log to a DB, log record pushed to error_log()',
66 +
                'level' => Logger::CRITICAL,
67 +
                'level_name' => 'critical',
68 +
                'context' => LogToDB::parseIfException(['exception' => $e]),
69 +
                'extra' => []
70 +
            ]);
71 +
            $this->emergencyLog($record);
73 72
        }
74 73
    }
74 +
75 +
    protected function emergencyLog(array $record)
76 +
    {
77 +
        $errorHandler = new ErrorLogHandler();
78 +
        $errorHandler->setFormatter(new LineFormatter('%level_name%: %message% %context%'));
79 +
        $errorHandler->handle($record);
80 +
    }
75 81
}

@@ -4,7 +4,6 @@
Loading
4 4
5 5
use danielme85\LaravelLogToDB\Jobs\SaveNewLogEvent;
6 6
use danielme85\LaravelLogToDB\Models\DBLog;
7 -
use danielme85\LaravelLogToDB\Models\DBLogException;
8 7
use danielme85\LaravelLogToDB\Models\DBLogMongoDB;
9 8
10 9
/**
@@ -147,7 +146,7 @@
Loading
147 146
148 147
        if (!empty($this->connection)) {
149 148
            if ($detailed && !empty($record['context']) && !empty($record['context']['exception'])) {
150 -
                $record['context'] = $this->parseIfException($record['context']);
149 +
                $record['context'] = self::parseIfException($record['context'], true);
151 150
            } else if (!$detailed) {
152 151
                $record['context'] = null;
153 152
            }
@@ -193,10 +192,11 @@
Loading
193 192
    /**
194 193
     * Parse the exception class
195 194
     *
196 -
     * @param mixed $context
195 +
     * @param array $context
196 +
     * @param bool $trace
197 197
     * @return array
198 198
     */
199 -
    private function parseIfException($context)
199 +
    public static function parseIfException(array $context, bool $trace = false)
200 200
    {
201 201
        if (!empty($context['exception'])) {
202 202
            $exception = $context['exception'];
@@ -222,7 +222,7 @@
Loading
222 222
                    if (method_exists($exception, 'getLine')) {
223 223
                        $newexception['line'] = $exception->getLine();
224 224
                    }
225 -
                    if (method_exists($exception, 'getTrace')) {
225 +
                    if ($trace && method_exists($exception, 'getTraceAsString')) {
226 226
                        $newexception['trace'] = $exception->getTraceAsString();
227 227
                    }
228 228
                    if (method_exists($exception, 'getSeverity')) {
Files Complexity Coverage
src 116 97.32%
Project Totals (12 files) 116 97.32%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading