Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log event context in the EventLog #1098

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/system/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ protected function registerLogging()
{
Event::listen(\Illuminate\Log\Events\MessageLogged::class, function ($event) {
if (EventLog::useLogging()) {
EventLog::add($event->message, $event->level);
$details = $event->context ?? null;
EventLog::add($event->message, $event->level, $details);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion modules/system/models/EventLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class_exists('Model') &&
/**
* Creates a log record
*/
public static function add(string $message, string $level = 'info', ?string $details = null): self
public static function add(string $message, string $level = 'info', ?array $details = null): self
{
$record = new static;
$record->message = $message;
Expand Down
28 changes: 28 additions & 0 deletions modules/system/tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace System\Tests;

use Db;
use Log;
use System\Tests\Bootstrap\PluginTestCase;

class ServiceProviderTest extends PluginTestCase
{
/**
* Test the registerLogging method
*
* @return void
*/
public function testRegisterLogging()
{
// Verify that calling the Log::info() method and passing in details stores those details in the event log table
$message = 'This is a test log message';
$details = [
'key' => 'Dummy value',
];
Log::info($message, $details);
$latestLog = Db::table('system_event_logs')->latest()->first();
$this->assertEquals($message, $latestLog->message);
$this->assertEquals($details, json_decode($latestLog->details, true));
}
}
Loading