foldershare-8.x-1.2/src/ManageLog.php

src/ManageLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
 
namespace Drupal\foldershare;
 
use Drupal\user\Entity\User;
 
/**
 * Manages logging for the FolderShare module.
 *
 * This class provides static methods to manage logging of activities
 * and errors by this module. Supported operations include:
 * - Posting an activity message to the log.
 * - Posting an error message to the log.
 *
 * This method also provides methods to query configuration settings
 * that enable/disable activity logging.
 *
 * <B>Access control</B>
 * This class's methods do not do access control. The caller should check
 * access as needed by their situation.
 *
 * @ingroup foldershare
 *
 * @see \Drupal\foldershare\Settings
 * @see \Drupal\foldershare\Entity\FolderShare
 */
trait ManageLog {
 
  /*---------------------------------------------------------------------
   *
   * Configuration.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Returns TRUE if the module's activity logging is enabled.
   *
   * When disabled, the module should not post activity messages to the
   * log. Error messages are still posted.
   *
   * @return bool
   *   Returns TRUE if activity logging is enabled.
   *
   * @see \Drupal\foldershare\Settings::getActivityLogEnable()
   */
  public static function isActivityLoggingEnabled() {
    return Settings::getActivityLogEnable();
  }
 
  /**
   * Returns the module title.
   *
   * The module title is a mixed case name, rather than the lowercase
   * machine name.
   *
   * @return string
   *   The module title.
   */
  private static function getModuleTitle() {
    // Cache the module title since it is unlikely to change.
    static $title = '';
    if ($title === '') {
      $moduleHandler = \Drupal::service('module_handler');
      $title = $moduleHandler->getName(Constants::MODULE);
    }
 
    return $title;
  }
 
  /*---------------------------------------------------------------------
   *
   * Logging.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Posts an activity message to the log.
   *
   * If the module's activity logging has been disabled, no message is posted.
   * Otherwise a 'notice' message is posted.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::isActivityLoggingEnabled()
   * @see ::log()
   */
  public static function activity(string $message, array $context = []) {
    if (self::isActivityLoggingEnabled() === FALSE) {
      return;
    }
 
    self::log('notice', $message, $context);
  }
 
  /**
   * Posts an alert message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function alert(string $message, array $context = []) {
    self::log('alert', $message, $context);
  }
 
  /**
   * Posts a critical message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function critical(string $message, array $context = []) {
    self::log('critical', $message, $context);
  }
 
  /**
   * Posts a debug message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function debug(string $message, array $context = []) {
    self::log('debug', $message, $context);
  }
 
  /**
   * Posts an emergency message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function emergency(string $message, array $context = []) {
    self::log('emergency', $message, $context);
  }
 
  /**
   * Posts an error message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function error(string $message, array $context = []) {
    self::log('error', $message, $context);
  }
 
  /**
   * Posts an error message about an exception to the log.
   *
   * The exception's type, message, and stack trace are added to the log.
   *
   * @param \Exception $e
   *   The exception.
   * @param int $uid
   *   (optional, default = (-1) = current user) The user ID of the user
   *   associated with the activity.
   */
  public static function exception(\Exception $e, int $uid = (-1)) {
    if ($e === NULL) {
      return;
    }
 
    $context = [
      'exception' => $e,
      'uid'       => $uid,
    ];
 
    self::log('error', '', $context);
  }
 
  /**
   * Posts an info message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function info(string $message, array $context = []) {
    self::log('info', $message, $context);
  }
 
  /**
   * Posts a notice message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function notice(string $message, array $context = []) {
    self::log('notice', $message, $context);
  }
 
  /**
   * Posts a warning message to the log.
   *
   * See log() for a detailed explanation of method arguments.
   *
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) The message context.
   *
   * @see ::log()
   */
  public static function warning(string $message, array $context = []) {
    self::log('warning', $message, $context);
  }
 
  /**
   * Posts a message to the log.
   *
   * If the message is empty, no message is logged.
   *
   * The severity level is expected to be one of the standard values:
   *   - "alert".
   *   - "critical".
   *   - "debug".
   *   - "emergency".
   *   - "error".
   *   - "info".
   *   - "notice".
   *   - "warning".
   *
   * If the severity level is empty, it defaults to 'notice'.
   *
   * If the message includes embedded carriage returns, they are replaced
   * with an HTML "<BR>" and carriage return so that the lines format better
   * on the message's detail page.
   *
   * Messages may contain variables of the form @name or %name that will be
   * replaced by their corresponding values for keys in the $context
   * associative array.
   *
   * The $context array may include substitution variables along with
   * special values processed by this method:
   *
   *   - "exception": An \Exception object. When present, the exception's
   *     type, message, and backtrace are appended to the given message.
   *
   *   - "entity": An entity, such as a FolderShare entity. When present,
   *     a link to the entity's page is added to the context as a "link"
   *     value that the logging system automatically adds into the
   *     "Operation" column of the standard log message page.
   *
   *   - "uid": The user performing the operation that led to the log
   *     message. When present, the message's user is set and the logging
   *     system automatically adds their name and a lnk into the "User"
   *     column of the standard log message page.
   *
   * The $context array may include additional special values processed by
   * the logging system:
   *
   *   - "channel": The message channel or type. This is automatically set
   *     to the module title, but any value in "channel" overrides this
   *     title.
   *
   *   - "timestamp": The log message time. This is automatically set to
   *     the current time.
   *
   * Additional values are automatically set by the logging system:
   *
   *   - "ip": The request IP address.
   *
   *   - "referer": The request referer.
   *
   *   - 'request_uri': The request URI.
   *
   * @param string $level
   *   The log level.
   * @param string $message
   *   The message to be logged.
   * @param array $context
   *   (optional, default = []) An associative array that provides mappings
   *   from keys to values.
   */
  public static function log(
    string $level,
    string $message,
    array $context = []) {
 
    // Default to a low-priority message.
    if (empty($level) === TRUE) {
      $level = 'notice';
    }
 
    // If an exception is given, create message text including the
    // exception type, exception message, and stack trace.
    $em = '';
    if (isset($context['exception']) === TRUE &&
        $context['exception'] !== NULL &&
        $context['exception'] instanceof \Exception === TRUE) {
      $e = $context['exception'];
      unset($context['exception']);
 
      $context['%exceptionType'] = get_class($e);
      $context['%exceptionMessage'] = $e->getMessage();
      $context['@exceptionBacktrace'] = $e->getTraceAsString();
      $em = "%exceptionType: %exceptionMessage <BR>\n<PRE>@exceptionBacktrace</PRE>";
    }
 
    // If there is neither a given message or an exception, then do nothing.
    if (empty($message) === TRUE && empty($em) === TRUE) {
      return;
    }
 
    // Replace carriage returns with line breaks for better formatting
    // on a log detail page.
    $m = mb_ereg_replace("\n", " <BR>\n", $message);
    if ($m === FALSE) {
      $m = $message;
    }
 
    // Add the exception message, if any.
    $m .= $em;
 
    // If an entity is given, use it for the log message link.
    if (isset($context['entity']) === TRUE &&
        isset($context['link']) === FALSE &&
        $context['entity'] !== NULL &&
        method_exists($context['entity'], 'toLink') === TRUE) {
      $entity = $context['entity'];
      unset($context['entity']);
 
      $context['link'] = $entity->toLink('View')->toString();
 
      $m .= " <BR>\nPath: @path";
      $context['@path'] = $entity->getPath();
    }
 
    $logger = \Drupal::logger(self::getModuleTitle());
 
    // Set the current user for purposes of logging.
    if (isset($context['uid']) === TRUE &&
        (int) $context['uid'] >= 0) {
      $currentUser = \Drupal::currentUser();
      if ((int) $context['uid'] === (int) $currentUser->id()) {
        $logger->setCurrentUser($currentUser);
      }
      else {
        $logger->setCurrentUser(User::load($context['uid']));
      }
    }
 
    $logger->log($level, $m, $context);
  }
 
  /*---------------------------------------------------------------------
   *
   * Standard messages.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Logs a standard error message about a missing scheduled task parameter.
   *
   * @param string $taskName
   *   The name of the task reporting the error.
   * @param string $parameterName
   *   The name of the parameter that is missing.
   */
  public static function missingTaskParameter(
    string $taskName,
    string $parameterName) {
 
    self::error(
      "Programmer error: Missing or bad parameter '@parameterName' for task '@taskName'.",
      [
        '@parameterName' => $parameterName,
        '@taskName'      => $taskName,
      ]);
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc