Skip to content

Commit

Permalink
More code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielme85 committed Apr 30, 2020
1 parent 0324a92 commit c6bd4d6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 76 deletions.
26 changes: 11 additions & 15 deletions src/LogToDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use danielme85\LaravelLogToDB\Jobs\SaveNewLogEvent;
use danielme85\LaravelLogToDB\Models\DBLog;
use danielme85\LaravelLogToDB\Models\DBLogException;
use danielme85\LaravelLogToDB\Models\DBLogMongoDB;

/**
Expand Down Expand Up @@ -71,15 +72,9 @@ function __construct($loggingConfig = [])
}

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

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

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

if (!empty($channel)) {
$channels = config('logging.channels');
if (isset($channels[$channel])) {
if (isset($channels[$channel]['connection']) and !empty($channels[$channel]['connection'])) {
if (!empty($channels[$channel])) {
if (!empty($channels[$channel]['connection'])) {
$conn = $channels[$channel]['connection'];
}
if (isset($channels[$channel]['collection']) and !empty($channels[$channel]['collection'])) {
if (!empty($channels[$channel]['collection'])) {
$coll = $channels[$channel]['collection'];
}
}
} else {
if (!empty($connection)) {
$conn = $connection;
}
$conn = $connection;
if (!empty($collection)) {
$coll = $collection;
}
Expand Down Expand Up @@ -155,8 +148,11 @@ public function newFromMonolog(array $record)
{
if (!empty($this->connection)) {
if ($this->config['queue']) {
if (isset($record['context']['exception']) && !empty($record['context']['exception'])) {
if (strpos(get_class($record['context']['exception']), "Exception") !== false) {
if (!empty($record['context']['exception'])) {
//Check for exception, they can't be queued.
$exception = $record['context']['exception'];
if (get_class($exception) === \Exception::class
|| is_subclass_of($exception, \Exception::class)) {
dispatch_now(new SaveNewLogEvent($this, $record));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/LogToDbCustomLoggingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function write(array $record): void
{
if (!empty($record)) {
if (!empty($record['context']['exception'])
&& strpos(get_class($record['context']['exception']), "DBLogException")) {
&& get_class($record['context']['exception']) === DBLogException::class) {
//Do nothing if empty log record or an error Exception from itself.
} else {
try {
Expand Down
64 changes: 8 additions & 56 deletions src/Models/LogToDbCreateObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,6 @@ public function generate(array $record, bool $detailed = false)
return $this;
}

/**
* @param $value
*/
public function setMessageAttribute($value)
{
if (config('logtodb.encrypt')) {
$this->attributes['message'] = encrypt($value);
} else {
$this->attributes['message'] = $value;
}
}

public function getMessageAttribute($value)
{
if (config('logtodb.encrypt')) {
return decrypt($value) ?? $value;
}

return $value;
}
/**
* Context Accessor
*
Expand Down Expand Up @@ -103,44 +83,16 @@ public function setContextAttribute(array $value)
if (isset($value['exception'])) {
if (!empty($value['exception'])) {
$exception = $value['exception'];
if (strpos(get_class($exception), "Exception") !== false) {
if (get_class($exception) === \Exception::class
|| is_subclass_of($exception, \Exception::class)) {
$newexception = [];
$newexception['class'] = get_class($exception);
if (method_exists($exception, 'getMessage')) {
$newexception['message'] = $exception->getMessage();
} else {
$newexception['message'] = null;
}
if (method_exists($exception, 'getCode')) {
$newexception['code'] = $exception->getCode();
} else {
$newexception['code'] = null;
}
if (method_exists($exception, 'getFile')) {
$newexception['file'] = $exception->getFile();
} else {
$newexception['file'] = null;
}
if (method_exists($exception, 'getLine')) {
$newexception['line'] = $exception->getLine();
} else {
$newexception['line'] = null;
}
if (method_exists($exception, 'getTrace')) {
$newexception['trace'] = $exception->getTrace();
} else {
$newexception['trace'] = null;
}
if (method_exists($exception, 'getPrevious')) {
$newexception['previous'] = $exception->getPrevious();
} else {
$newexception['previous'] = null;
}
if (method_exists($exception, 'getSeverity')) {
$newexception['severity'] = $exception->getSeverity();
} else {
$newexception['severity'] = null;
}
$newexception['message'] = $exception->getMessage();
$newexception['code'] = $exception->getCode();
$newexception['file'] = $exception->getFile();
$newexception['line'] = $exception->getLine();
$newexception['trace'] = $exception->getTrace();
$newexception['previous'] = $exception->getPrevious();

$value['exception'] = $newexception;
}
Expand Down
42 changes: 38 additions & 4 deletions tests/LogToDbTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use danielme85\LaravelLogToDB\LogToDB;
use danielme85\LaravelLogToDB\Models\DBLogException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use danielme85\LaravelLogToDB\Jobs\SaveNewLogEvent;
Expand All @@ -15,7 +16,6 @@ protected function setUp(): void
parent::setUp();

$this->artisan('migrate', ['--database' => 'mysql']);

}

/**
Expand Down Expand Up @@ -121,14 +121,31 @@ protected function getPackageProviders($app)
*/
public function testClassInit()
{
$test = new LogToDB();
$this->assertInstanceOf('danielme85\LaravelLogToDB\LogToDB', $test);
$this->assertInstanceOf(LogToDB::class, app('laravel-log-to-db'));
$this->assertInstanceOf(LogToDB::class, new LogToDB());

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


/**
* @group config
*/
public function testMissingConfig()
{
config()->set('database.default', 'bajs');
config()->set('logging.default', 'nein');

$this->expectException(DBLogException::class);
Log::info("Imma gonna faila?");

$this->expectException(InvalidArgumentException::class);
$logReader = LogToDB::model('spise', 'kebab', 'hverdag')->get()->toArray();
$this->assertEmpty($logReader);
}

/**
* Run basic log levels
*
Expand Down Expand Up @@ -198,14 +215,31 @@ public function testLoggingToChannels()
/**
* Test an exception error.
*
* @group advanced
* @group exception
*/
public function testException()
{
$e = new Symfony\Component\HttpKernel\Exception\BadRequestHttpException("This is a fake 500 error", null, 500, ['fake-header' => 'value']);
Log::warning("Error", ['exception' => $e, 'more' => 'infohere']);
$log = LogToDB::model()->where('message', 'Error')->first();
$this->assertNotEmpty($log->context);

$empty = new \Mockery\Exception();
Log::warning("Error", ['exception' => $empty]);
$log = LogToDB::model()->where('message', 'Error')->orderBy('id', 'DESC')->first();
$this->assertNotEmpty($log);

$this->expectException(DBLogException::class);
throw new DBLogException('Dont log this');
}

/**
*
* @group exception
*/
public function testExceptionIgnore()
{
$this->assertCount(0, LogToDB::model()->where('message', '=', 'Dont log this')->get()->toArray());
}

/**
Expand Down

0 comments on commit c6bd4d6

Please sign in to comment.