2
2
3
3
namespace Drupal \os2web_audit \Service ;
4
4
5
- use Drupal \Component \Plugin \Exception \PluginException ;
6
5
use Drupal \Core \Config \ConfigFactoryInterface ;
6
+ use Drupal \Core \Entity \EntityTypeManagerInterface ;
7
7
use Drupal \Core \Logger \LoggerChannelFactoryInterface ;
8
8
use Drupal \Core \Session \AccountProxyInterface ;
9
- use Drupal \os2web_audit \Exception \AuditException ;
10
- use Drupal \os2web_audit \Exception \ConnectionException ;
9
+ use Drupal \advancedqueue \Job ;
11
10
use Drupal \os2web_audit \Form \PluginSettingsForm ;
12
11
use Drupal \os2web_audit \Form \SettingsForm ;
12
+ use Drupal \os2web_audit \Plugin \AdvancedQueue \JobType \LogMessages ;
13
13
use Drupal \os2web_audit \Plugin \LoggerManager ;
14
14
use Symfony \Component \HttpFoundation \RequestStack ;
15
15
20
20
*/
21
21
class Logger {
22
22
23
+ const string OS2WEB_AUDIT_QUEUE_ID = 'os2web_audit ' ;
24
+ const string OS2WEB_AUDIT_LOGGER_CHANNEL = 'os2web_audit_info ' ;
25
+
23
26
public function __construct (
24
27
private readonly LoggerManager $ loggerManager ,
25
28
private readonly ConfigFactoryInterface $ configFactory ,
26
29
private readonly AccountProxyInterface $ currentUser ,
27
30
private readonly LoggerChannelFactoryInterface $ watchdog ,
28
31
private readonly RequestStack $ requestStack ,
32
+ private readonly EntityTypeManagerInterface $ entityTypeManager ,
29
33
) {
30
34
}
31
35
@@ -43,7 +47,7 @@ public function __construct(
43
47
* Additional metadata for the log message. Default is an empty array.
44
48
*/
45
49
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 ' ]);
47
51
}
48
52
49
53
/**
@@ -60,11 +64,11 @@ public function info(string $type, string $line, bool $logUser = TRUE, array $me
60
64
* Additional metadata for the log message. Default is an empty array.
61
65
*/
62
66
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 ' ]);
64
68
}
65
69
66
70
/**
67
- * Logs a message using a plugin-specific logger .
71
+ * Creates and enqueues logging job .
68
72
*
69
73
* @param string $type
70
74
* 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
78
82
* @param array<string, string> $metadata
79
83
* Additional metadata for the log message. Default is an empty array.
80
84
*/
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 {
85
86
87
+ // Enhance logging data with current user and current request information.
86
88
if ($ logUser ) {
87
89
// Add user id to the log message metadata.
88
90
$ metadata ['userId ' ] = $ this ->currentUser ->getEmail ();
@@ -95,25 +97,57 @@ private function log(string $type, int $timestamp, string $line, bool $logUser =
95
97
$ line .= sprintf (' Remote ip: %s ' , $ ip_address );
96
98
}
97
99
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
+
98
111
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 );
105
119
}
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 );
116
122
}
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 );
117
151
}
118
152
119
153
}
0 commit comments