22
33namespace Drupal \os2web_audit \Service ;
44
5- use Drupal \Component \Plugin \Exception \PluginException ;
65use Drupal \Core \Config \ConfigFactoryInterface ;
6+ use Drupal \Core \Entity \EntityTypeManagerInterface ;
77use Drupal \Core \Logger \LoggerChannelFactoryInterface ;
88use Drupal \Core \Session \AccountProxyInterface ;
9- use Drupal \os2web_audit \Exception \AuditException ;
10- use Drupal \os2web_audit \Exception \ConnectionException ;
9+ use Drupal \advancedqueue \Job ;
1110use Drupal \os2web_audit \Form \PluginSettingsForm ;
1211use Drupal \os2web_audit \Form \SettingsForm ;
12+ use Drupal \os2web_audit \Plugin \AdvancedQueue \JobType \LogMessages ;
1313use Drupal \os2web_audit \Plugin \LoggerManager ;
1414use Symfony \Component \HttpFoundation \RequestStack ;
1515
2020 */
2121class Logger {
2222
23+ const string OS2WEB_AUDIT_QUEUE_ID = 'os2web_audit ' ;
24+ const string OS2WEB_AUDIT_LOGGER_CHANNEL = 'os2web_audit_info ' ;
25+
2326 public function __construct (
2427 private readonly LoggerManager $ loggerManager ,
2528 private readonly ConfigFactoryInterface $ configFactory ,
2629 private readonly AccountProxyInterface $ currentUser ,
2730 private readonly LoggerChannelFactoryInterface $ watchdog ,
2831 private readonly RequestStack $ requestStack ,
32+ private readonly EntityTypeManagerInterface $ entityTypeManager ,
2933 ) {
3034 }
3135
@@ -43,7 +47,7 @@ public function __construct(
4347 * Additional metadata for the log message. Default is an empty array.
4448 */
4549 public function info (string $ type , string $ line , bool $ logUser = TRUE , array $ metadata = []): void {
46- $ this ->log ($ type , time (), $ line , $ logUser , $ metadata + ['level ' => 'info ' ]);
50+ $ this ->createLoggingJob ($ type , time (), $ line , $ logUser , $ metadata + ['level ' => 'info ' ]);
4751 }
4852
4953 /**
@@ -60,11 +64,11 @@ public function info(string $type, string $line, bool $logUser = TRUE, array $me
6064 * Additional metadata for the log message. Default is an empty array.
6165 */
6266 public function error (string $ type , string $ line , bool $ logUser = TRUE , array $ metadata = []): void {
63- $ this ->log ($ type , time (), $ line , $ logUser , $ metadata + ['level ' => 'error ' ]);
67+ $ this ->createLoggingJob ($ type , time (), $ line , $ logUser , $ metadata + ['level ' => 'error ' ]);
6468 }
6569
6670 /**
67- * Logs a message using a plugin-specific logger .
71+ * Creates and enqueues logging job .
6872 *
6973 * @param string $type
7074 * The type of event to log (auth, lookup etc.)
@@ -78,11 +82,9 @@ public function error(string $type, string $line, bool $logUser = TRUE, array $m
7882 * @param array<string, string> $metadata
7983 * Additional metadata for the log message. Default is an empty array.
8084 */
81- private function log (string $ type , int $ timestamp , string $ line , bool $ logUser = FALSE , array $ metadata = []): void {
82- $ config = $ this ->configFactory ->get (SettingsForm::$ configName );
83- $ plugin_id = $ config ->get ('provider ' ) ?? SettingsForm::OS2WEB_AUDIT_DEFUALT_PROVIDER ;
84- $ configuration = $ this ->configFactory ->get (PluginSettingsForm::getConfigName ())->get ($ plugin_id );
85+ private function createLoggingJob (string $ type , int $ timestamp , string $ line , bool $ logUser = FALSE , array $ metadata = []): void {
8586
87+ // Enhance logging data with current user and current request information.
8688 if ($ logUser ) {
8789 // Add user id to the log message metadata.
8890 $ metadata ['userId ' ] = $ this ->currentUser ->getEmail ();
@@ -95,25 +97,57 @@ private function log(string $type, int $timestamp, string $line, bool $logUser =
9597 $ line .= sprintf (' Remote ip: %s ' , $ ip_address );
9698 }
9799
100+ $ config = $ this ->configFactory ->get (SettingsForm::$ configName );
101+ $ plugin_id = $ config ->get ('provider ' ) ?? SettingsForm::OS2WEB_AUDIT_DEFUALT_PROVIDER ;
102+
103+ $ payload = [
104+ 'type ' => $ type ,
105+ 'timestamp ' => $ timestamp ,
106+ 'line ' => $ line ,
107+ 'plugin_id ' => $ plugin_id ,
108+ 'metadata ' => $ metadata ,
109+ ];
110+
98111 try {
99- /** @var \Drupal\os2web_audit\Plugin\AuditLogger\AuditLoggerInterface $logger */
100- $ logger = $ this ->loggerManager ->createInstance ($ plugin_id , $ configuration ?? []);
101- $ logger ->log ($ type , $ timestamp , $ line , $ metadata );
102- }
103- catch (PluginException $ e ) {
104- $ this ->watchdog ->get ('os2web_audit ' )->error ($ e ->getMessage ());
112+ $ queueStorage = $ this ->entityTypeManager ->getStorage ('advancedqueue_queue ' );
113+ /** @var \Drupal\advancedqueue\Entity\Queue $queue */
114+ $ queue = $ queueStorage ->load (self ::OS2WEB_AUDIT_QUEUE_ID );
115+
116+ $ job = Job::create (LogMessages::class, $ payload );
117+
118+ $ queue ->enqueueJob ($ job );
105119 }
106- catch (AuditException | ConnectionException $ e ) {
107- // Change metadata into string.
108- $ data = implode (', ' , array_map (function ($ key , $ value ) {
109- return $ key . " => " . $ value ;
110- }, array_keys ($ metadata ), $ metadata ));
111-
112- // Fallback to send log message info watchdog.
113- $ msg = sprintf ("Plugin: %s, Type: %s, Msg: %s, Metadata: %s " , $ e ->getPluginName (), $ type , $ line , $ data );
114- $ this ->watchdog ->get ('os2web_audit ' )->info ($ msg );
115- $ this ->watchdog ->get ('os2web_audit_error ' )->error ($ e ->getMessage ());
120+ catch (\Exception $ exception ) {
121+ $ this ->watchdog ->get (self ::OS2WEB_AUDIT_LOGGER_CHANNEL )->error (sprintf ('Failed creating job: %s ' , $ exception ->getMessage ()), $ payload );
116122 }
123+
124+ }
125+
126+ /**
127+ * Logs a message using a plugin-specific logger.
128+ *
129+ * @param string $type
130+ * The type of event to log (auth, lookup etc.)
131+ * @param int $timestamp
132+ * The timestamp for the log message.
133+ * @param string $line
134+ * The log message.
135+ * @param string $plugin_id
136+ * The logging plugin id.
137+ * @param array<string, string> $metadata
138+ * Additional metadata for the log message. Default is an empty array.
139+ *
140+ * @throws \Drupal\Component\Plugin\Exception\PluginException
141+ * @throws \Drupal\os2web_audit\Exception\ConnectionException
142+ * @throws \Drupal\os2web_audit\Exception\AuditException
143+ */
144+ public function log (string $ type , int $ timestamp , string $ line , string $ plugin_id , array $ metadata = []): void {
145+
146+ $ configuration = $ this ->configFactory ->get (PluginSettingsForm::getConfigName ())->get ($ plugin_id );
147+
148+ /** @var \Drupal\os2web_audit\Plugin\AuditLogger\AuditLoggerInterface $logger */
149+ $ logger = $ this ->loggerManager ->createInstance ($ plugin_id , $ configuration ?? []);
150+ $ logger ->log ($ type , $ timestamp , $ line , $ metadata );
117151 }
118152
119153}
0 commit comments