Skip to content

Commit c6bd4d6

Browse files
committed
More code cleanup.
1 parent 0324a92 commit c6bd4d6

File tree

4 files changed

+58
-76
lines changed

4 files changed

+58
-76
lines changed

src/LogToDB.php

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

55
use danielme85\LaravelLogToDB\Jobs\SaveNewLogEvent;
66
use danielme85\LaravelLogToDB\Models\DBLog;
7+
use danielme85\LaravelLogToDB\Models\DBLogException;
78
use danielme85\LaravelLogToDB\Models\DBLogMongoDB;
89

910
/**
@@ -71,15 +72,9 @@ function __construct($loggingConfig = [])
7172
}
7273

7374
if (empty($this->database)) {
74-
new \ErrorException("Required configs missing: The LogToDB class needs a database correctly setup in the configs: databases.php and logtodb.php");
75+
new DBLogException("Required configs missing: The LogToDB class needs a database correctly setup in the configs: databases.php and logtodb.php");
7576
}
7677

77-
//If the string 'default' is set for queue connection, then set null as this defaults to 'default' anyways.
78-
if (!empty($this->config['queue'])) {
79-
if ($this->config['queue_name'] === 'default') {
80-
$this->config['queue_name'] = null;
81-
}
82-
}
8378
}
8479

8580
/**
@@ -98,18 +93,16 @@ public static function model(string $channel = null, string $connection = 'defau
9893

9994
if (!empty($channel)) {
10095
$channels = config('logging.channels');
101-
if (isset($channels[$channel])) {
102-
if (isset($channels[$channel]['connection']) and !empty($channels[$channel]['connection'])) {
96+
if (!empty($channels[$channel])) {
97+
if (!empty($channels[$channel]['connection'])) {
10398
$conn = $channels[$channel]['connection'];
10499
}
105-
if (isset($channels[$channel]['collection']) and !empty($channels[$channel]['collection'])) {
100+
if (!empty($channels[$channel]['collection'])) {
106101
$coll = $channels[$channel]['collection'];
107102
}
108103
}
109104
} else {
110-
if (!empty($connection)) {
111-
$conn = $connection;
112-
}
105+
$conn = $connection;
113106
if (!empty($collection)) {
114107
$coll = $collection;
115108
}
@@ -155,8 +148,11 @@ public function newFromMonolog(array $record)
155148
{
156149
if (!empty($this->connection)) {
157150
if ($this->config['queue']) {
158-
if (isset($record['context']['exception']) && !empty($record['context']['exception'])) {
159-
if (strpos(get_class($record['context']['exception']), "Exception") !== false) {
151+
if (!empty($record['context']['exception'])) {
152+
//Check for exception, they can't be queued.
153+
$exception = $record['context']['exception'];
154+
if (get_class($exception) === \Exception::class
155+
|| is_subclass_of($exception, \Exception::class)) {
160156
dispatch_now(new SaveNewLogEvent($this, $record));
161157
}
162158
}

src/LogToDbCustomLoggingHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function write(array $record): void
5656
{
5757
if (!empty($record)) {
5858
if (!empty($record['context']['exception'])
59-
&& strpos(get_class($record['context']['exception']), "DBLogException")) {
59+
&& get_class($record['context']['exception']) === DBLogException::class) {
6060
//Do nothing if empty log record or an error Exception from itself.
6161
} else {
6262
try {

src/Models/LogToDbCreateObject.php

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,6 @@ public function generate(array $record, bool $detailed = false)
5151
return $this;
5252
}
5353

54-
/**
55-
* @param $value
56-
*/
57-
public function setMessageAttribute($value)
58-
{
59-
if (config('logtodb.encrypt')) {
60-
$this->attributes['message'] = encrypt($value);
61-
} else {
62-
$this->attributes['message'] = $value;
63-
}
64-
}
65-
66-
public function getMessageAttribute($value)
67-
{
68-
if (config('logtodb.encrypt')) {
69-
return decrypt($value) ?? $value;
70-
}
71-
72-
return $value;
73-
}
7454
/**
7555
* Context Accessor
7656
*
@@ -103,44 +83,16 @@ public function setContextAttribute(array $value)
10383
if (isset($value['exception'])) {
10484
if (!empty($value['exception'])) {
10585
$exception = $value['exception'];
106-
if (strpos(get_class($exception), "Exception") !== false) {
86+
if (get_class($exception) === \Exception::class
87+
|| is_subclass_of($exception, \Exception::class)) {
10788
$newexception = [];
10889
$newexception['class'] = get_class($exception);
109-
if (method_exists($exception, 'getMessage')) {
110-
$newexception['message'] = $exception->getMessage();
111-
} else {
112-
$newexception['message'] = null;
113-
}
114-
if (method_exists($exception, 'getCode')) {
115-
$newexception['code'] = $exception->getCode();
116-
} else {
117-
$newexception['code'] = null;
118-
}
119-
if (method_exists($exception, 'getFile')) {
120-
$newexception['file'] = $exception->getFile();
121-
} else {
122-
$newexception['file'] = null;
123-
}
124-
if (method_exists($exception, 'getLine')) {
125-
$newexception['line'] = $exception->getLine();
126-
} else {
127-
$newexception['line'] = null;
128-
}
129-
if (method_exists($exception, 'getTrace')) {
130-
$newexception['trace'] = $exception->getTrace();
131-
} else {
132-
$newexception['trace'] = null;
133-
}
134-
if (method_exists($exception, 'getPrevious')) {
135-
$newexception['previous'] = $exception->getPrevious();
136-
} else {
137-
$newexception['previous'] = null;
138-
}
139-
if (method_exists($exception, 'getSeverity')) {
140-
$newexception['severity'] = $exception->getSeverity();
141-
} else {
142-
$newexception['severity'] = null;
143-
}
90+
$newexception['message'] = $exception->getMessage();
91+
$newexception['code'] = $exception->getCode();
92+
$newexception['file'] = $exception->getFile();
93+
$newexception['line'] = $exception->getLine();
94+
$newexception['trace'] = $exception->getTrace();
95+
$newexception['previous'] = $exception->getPrevious();
14496

14597
$value['exception'] = $newexception;
14698
}

tests/LogToDbTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use danielme85\LaravelLogToDB\LogToDB;
4+
use danielme85\LaravelLogToDB\Models\DBLogException;
45
use Illuminate\Support\Facades\Log;
56
use Illuminate\Support\Facades\Queue;
67
use danielme85\LaravelLogToDB\Jobs\SaveNewLogEvent;
@@ -15,7 +16,6 @@ protected function setUp(): void
1516
parent::setUp();
1617

1718
$this->artisan('migrate', ['--database' => 'mysql']);
18-
1919
}
2020

2121
/**
@@ -121,14 +121,31 @@ protected function getPackageProviders($app)
121121
*/
122122
public function testClassInit()
123123
{
124-
$test = new LogToDB();
125-
$this->assertInstanceOf('danielme85\LaravelLogToDB\LogToDB', $test);
124+
$this->assertInstanceOf(LogToDB::class, app('laravel-log-to-db'));
125+
$this->assertInstanceOf(LogToDB::class, new LogToDB());
126126

127127
//Class works, now let's cleanup possible failed test
128128
LogToDB::model()->truncate();
129129
LogToDB::model('mongodb')->truncate();
130130
}
131131

132+
133+
/**
134+
* @group config
135+
*/
136+
public function testMissingConfig()
137+
{
138+
config()->set('database.default', 'bajs');
139+
config()->set('logging.default', 'nein');
140+
141+
$this->expectException(DBLogException::class);
142+
Log::info("Imma gonna faila?");
143+
144+
$this->expectException(InvalidArgumentException::class);
145+
$logReader = LogToDB::model('spise', 'kebab', 'hverdag')->get()->toArray();
146+
$this->assertEmpty($logReader);
147+
}
148+
132149
/**
133150
* Run basic log levels
134151
*
@@ -198,14 +215,31 @@ public function testLoggingToChannels()
198215
/**
199216
* Test an exception error.
200217
*
201-
* @group advanced
218+
* @group exception
202219
*/
203220
public function testException()
204221
{
205222
$e = new Symfony\Component\HttpKernel\Exception\BadRequestHttpException("This is a fake 500 error", null, 500, ['fake-header' => 'value']);
206223
Log::warning("Error", ['exception' => $e, 'more' => 'infohere']);
207224
$log = LogToDB::model()->where('message', 'Error')->first();
208225
$this->assertNotEmpty($log->context);
226+
227+
$empty = new \Mockery\Exception();
228+
Log::warning("Error", ['exception' => $empty]);
229+
$log = LogToDB::model()->where('message', 'Error')->orderBy('id', 'DESC')->first();
230+
$this->assertNotEmpty($log);
231+
232+
$this->expectException(DBLogException::class);
233+
throw new DBLogException('Dont log this');
234+
}
235+
236+
/**
237+
*
238+
* @group exception
239+
*/
240+
public function testExceptionIgnore()
241+
{
242+
$this->assertCount(0, LogToDB::model()->where('message', '=', 'Dont log this')->get()->toArray());
209243
}
210244

211245
/**

0 commit comments

Comments
 (0)