I installed Laravel audit with the following config:
<?php
use AppServicesAuditingResolversUserResolver;
use ModulesUserEntitiesAudit;
return [
'enabled' => env('AUDITING_ENABLED', true),
/*
|--------------------------------------------------------------------------
| Audit Implementation
|--------------------------------------------------------------------------
|
| Define which Audit model implementation should be used.
|
*/
'implementation' => Audit::class,
/*
|--------------------------------------------------------------------------
| User Morph prefix & Guards
|--------------------------------------------------------------------------
|
| Define the morph prefix and authentication guards for the User resolver.
|
*/
'user' => [
'morph_prefix' => 'operator',
'guards' => [
'web',
'api'
],
'resolver' => UserResolver::class
],
/*
|--------------------------------------------------------------------------
| Audit Resolvers
|--------------------------------------------------------------------------
|
| Define the IP Address, User Agent and URL resolver implementations.
|
*/
'resolvers' => [
'ip_address' => OwenItAuditingResolversIpAddressResolver::class,
'user_agent' => OwenItAuditingResolversUserAgentResolver::class,
'url' => OwenItAuditingResolversUrlResolver::class
],
/*
|--------------------------------------------------------------------------
| Audit Events
|--------------------------------------------------------------------------
|
| The Eloquent events that trigger an Audit.
|
*/
'events' => [
'created',
'updated',
'deleted',
'restored'
],
'strict' => false,
'exclude' => [],
'empty_values' => true,
'allowed_empty_values' => [
'retrieved'
],
'allowed_array_values' => false,
'timestamps' => true,
'threshold' => 0,
'driver' => 'database',
'drivers' => [
'database' => [
'table' => 'audits',
'connection' => null,
],
],
'queue' => [
'enable' => true,
'connection' => 'redis',
'queue' => 'audit',
'delay' => 0,
],
'console' => false,
];
Two models, Booking and Task, are equipped with the Auditable trait for logging purposes via Laravel Audit package.
When a Booking is created, an asynchronous listener triggered by an event is responsible for creating a Task record in the database. The issue arises when the Laravel Queue Connection is set to redis
. Despite successfully inserting records for both Booking and Task into the database, only the log for Booking gets recorded in the audits table. Strangely, the log for Task fails to be recorded. The horizon doesn’t even show that
OwenItAuditingListenersProcessDispatchAudit
is being fired for recording the log of the Task. Thus, I’m sure that the event for inserting the log of the Task is not fired in this case.
Interestingly, when the Laravel Queue Connection is switched to sync
, everything functions correctly. Both the creation of Booking and Task instances are effectively logged in the audits table.
How can I solve the issue?
I don’t know why, but
App::runningInConsole()
returns true and the following function prevents it from running
public static function isAuditingEnabled(): bool
{
if (App::runningInConsole()) {
return Config::get('audit.enabled', true) && Config::get('audit.console', false);
}
return Config::get('audit.enabled', true);
}
So I set
'console' => true,
and it worked. I would appreciate it if anybody tells me why App::runningInConsole()
is True when an asynchrony listener is running.
The URL is log is
artisan horizon:work redis --name=default --supervisor=5803372c5219-vUam:task --backoff=0 --max-time=0 --max-jobs=0 --memory=128 --queue=task --sleep=3 --timeout=60 --tries=3 --rest=0