Drupalのwatchdogでログを残す&使い方
Aug 14, 2015AD:
たとえば、ユーザが追加された際などにWatchDogでログを残しておく場合
1 2 3 4 5 6 7 8 9 |
watchdog('custom_reports', // Typeを指定 'New user: %name (%email).', //メッセージ array( '%name' => $form_state['values']['name'], //メッセージにデータを渡す '%email' => $form_state['values']['mail'] //メッセージにデータを渡す ), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit') // 実行された場所をしていできる ); |
結果はこんな感じ
確認ページはこちら
/admin/reports/dblog
変数の内容をWatchdogにのこしておきたい場合
1 |
watchdog('server_information', print_r($_SERVER, TRUE)); |
結果はこんな感じ
実際のFunctionはこんな感じです。
https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/watchdog/7
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 |
function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) { global $user, $base_root; static $in_error_state = FALSE; // It is possible that the error handling will itself trigger an error. In that case, we could // end up in an infinite loop. To avoid that, we implement a simple static semaphore. if (!$in_error_state && function_exists('module_implements')) { $in_error_state = TRUE; // The user object may not exist in all conditions, so 0 is substituted if needed. $user_uid = isset($user->uid) ? $user->uid : 0; // Prepare the fields to be logged $log_entry = array( 'type' => $type, 'message' => $message, 'variables' => $variables, 'severity' => $severity, 'link' => $link, 'user' => $user, 'uid' => $user_uid, 'request_uri' => $base_root . request_uri(), 'referer' => isset($_SERVER ['HTTP_REFERER']) ? $_SERVER ['HTTP_REFERER'] : '', 'ip' => ip_address(), // Request time isn't accurate for long processes, use time() instead. 'timestamp' => time(), ); // Call the logging hooks to log/process the message foreach (module_implements('watchdog') as $module) { module_invoke($module, 'watchdog', $log_entry); } // It is critical that the semaphore is only cleared here, in the parent // watchdog() call (not outside the loop), to prevent recursive execution. $in_error_state = FALSE; } } |
AD:
No Comments, Comment or Ping
Reply to “Drupalのwatchdogでログを残す&使い方”
Warning: Undefined variable $user_ID in /home/sazaeau/mizoshiri.com/public_html/blog.mizoshiri.com/wp-content/themes/grid_focus_public_mizo/comments.php on line 66
You must be logged in to post a comment.