Skip to content

Commit 53bdca8

Browse files
authored
Merge pull request #44 from danielme85/v3.1
Add changes from V3.1 branch to master.
2 parents fb99bd2 + 8251931 commit 53bdca8

13 files changed

+195
-37
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ version: 2.1
33
jobs:
44
test:
55
docker:
6-
- image: danielme/laravel-cli:latest
6+
- image: danielme/laravel-circleci-php7:latest
77
- image: circleci/mongo:latest
8-
- image: circleci/mysql:5.7
8+
- image: circleci/mariadb:latest
99
environment:
1010
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
1111
MYSQL_ROOT_PASSWORD: ''

.circleci/php7/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM alpine:latest
2+
3+
LABEL Maintainer="Daniel Mellum <mellum@gmail.com>" \
4+
Description="A simple docker image used in phpunit testing Laravel apps."
5+
6+
ENV DOCKERIZE_VERSION v0.6.1
7+
8+
RUN apk --no-cache add php7 php7-common php7-fpm php7-zip php7-json php7-openssl php7-curl \
9+
php7-zlib php7-xml php7-phar php7-intl php7-dom php7-xmlreader php7-xmlwriter php7-ctype \
10+
php7-mbstring php7-gd php7-session php7-pdo php7-pdo_mysql php7-tokenizer php7-posix \
11+
php7-fileinfo php7-opcache php7-cli php7-mcrypt php7-pcntl php7-iconv php7-simplexml php7-mongodb \
12+
curl git openssl openssh-client mysql-client bash
13+
14+
RUN apk add php7-pecl-pcov --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing
15+
16+
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
17+
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
18+
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz
19+
20+
RUN apk --no-cache add
21+
22+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
23+
24+
ENTRYPOINT ["/entrypoint.sh"]

.circleci/php7/entrypoint.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
export DEBIAN_FRONTEND=noninteractive
3+
4+
bash <(cat /etc/os-release; echo 'echo ${VERSION/*, /}')
5+
php -v
6+
nginx -v
7+
8+
echo "Entrypoint/Boot actions completed..."

.circleci/php8/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM alpine:latest
2+
3+
LABEL Maintainer="Daniel Mellum <mellum@gmail.com>" \
4+
Description="A simple docker image used in phpunit testing Laravel apps."
5+
6+
ENV DOCKERIZE_VERSION v0.6.1
7+
8+
RUN apk --no-cache add php8 php8-common php8-fpm php8-zip php8-json php8-openssl php8-curl \
9+
php8-zlib php8-xml php8-phar php8-intl php8-dom php8-xmlreader php8-xmlwriter php8-ctype \
10+
php8-mbstring php8-gd php8-session php8-pdo php8-pdo_mysql php8-tokenizer php8-posix \
11+
php8-fileinfo php8-opcache php8-cli php8-pcntl php8-iconv php8-simplexml php8-mongodb \
12+
curl git openssl openssh-client mysql-client bash
13+
14+
RUN apk add php8-pecl-pcov --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing
15+
16+
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
17+
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
18+
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz
19+
20+
RUN apk --no-cache add
21+
22+
RUN curl -sS https://getcomposer.org/installer | php8 -- --install-dir=/usr/local/bin --filename=composer
23+
24+
ENTRYPOINT ["/entrypoint.sh"]

.circleci/php8/entrypoint.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
export DEBIAN_FRONTEND=noninteractive
3+
4+
bash <(cat /etc/os-release; echo 'echo ${VERSION/*, /}')
5+
php -v
6+
nginx -v
7+
8+
echo "Entrypoint/Boot actions completed..."

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
},
1717
"require-dev": {
1818
"orchestra/testbench": ">=5.0",
19-
"mockery/mockery": "^1.0"
19+
"mockery/mockery": "^1.0",
20+
"nunomaduro/collision": "^5.10",
21+
"jenssegers/mongodb": "^3.8"
2022
},
2123
"suggest": {
2224
"jenssegers/mongodb": "Adds support for MongoDB in Laravel/Eloquent"

docker-compose.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
version: "2.4"
3+
services:
4+
mariadb:
5+
image: mariadb:latest
6+
container_name: mariadb_phpunit
7+
network_mode: bridge
8+
ports:
9+
- "3366:3306"
10+
environment:
11+
- MYSQL_DATABASE=testing
12+
- MYSQL_USER=testing
13+
- MYSQL_ROOT_PASSWORD=norestforthetest
14+
- MYSQL_PASSWORD=norestforthetest
15+
16+
mongo:
17+
image: mongo
18+
container_name: mongodb_phpunit
19+
restart: always
20+
network_mode: bridge
21+
ports:
22+
- "27888:27017"

src/Jobs/SaveNewLogEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace danielme85\LaravelLogToDB\Jobs;
44

5-
use danielme85\LaravelLogToDB\Models\CreateLogFromRecord;
65
use danielme85\LaravelLogToDB\Models\DBLogException;
76
use Illuminate\Bus\Queueable;
87
use Illuminate\Queue\InteractsWithQueue;

src/LogToDB.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use danielme85\LaravelLogToDB\Jobs\SaveNewLogEvent;
66
use danielme85\LaravelLogToDB\Models\DBLog;
7-
use danielme85\LaravelLogToDB\Models\DBLogException;
87
use danielme85\LaravelLogToDB\Models\DBLogMongoDB;
98

109
/**
@@ -147,7 +146,7 @@ public function newFromMonolog(array $record)
147146

148147
if (!empty($this->connection)) {
149148
if ($detailed && !empty($record['context']) && !empty($record['context']['exception'])) {
150-
$record['context'] = $this->parseIfException($record['context']);
149+
$record['context'] = self::parseIfException($record['context'], true);
151150
} else if (!$detailed) {
152151
$record['context'] = null;
153152
}
@@ -193,10 +192,11 @@ public function getConfig(string $config)
193192
/**
194193
* Parse the exception class
195194
*
196-
* @param mixed $context
195+
* @param array $context
196+
* @param bool $trace
197197
* @return array
198198
*/
199-
private function parseIfException($context)
199+
public static function parseIfException(array $context, bool $trace = false)
200200
{
201201
if (!empty($context['exception'])) {
202202
$exception = $context['exception'];
@@ -222,7 +222,7 @@ private function parseIfException($context)
222222
if (method_exists($exception, 'getLine')) {
223223
$newexception['line'] = $exception->getLine();
224224
}
225-
if (method_exists($exception, 'getTrace')) {
225+
if ($trace && method_exists($exception, 'getTraceAsString')) {
226226
$newexception['trace'] = $exception->getTraceAsString();
227227
}
228228
if (method_exists($exception, 'getSeverity')) {

src/LogToDbCustomLoggingHandler.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace danielme85\LaravelLogToDB;
44

55
use danielme85\LaravelLogToDB\Models\DBLogException;
6+
use Monolog\Formatter\LineFormatter;
67
use Monolog\Handler\AbstractProcessingHandler;
8+
use Monolog\Handler\ErrorLogHandler;
9+
use Monolog\Logger;
710

811
/**
912
* Class LogToDbHandler
@@ -54,22 +57,25 @@ function __construct(array $config,
5457
*/
5558
protected function write(array $record): void
5659
{
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);
7372
}
7473
}
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+
}
7581
}

0 commit comments

Comments
 (0)