0

function getIPAddresslog() {  
     if(!empty($_SERVER['HTTP_CLIENT_IP'])) {  
                $ip = $_SERVER['HTTP_CLIENT_IP'];  
        }  
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
     }   
    else{  
             $ip = $_SERVER['REMOTE_ADDR'];  
     }  
     return $ip;  
}  
if (!function_exists('write_log')) {
 function write_log($log,$action="",$type="custom") {
  $logged_in_user = get_current_user_id(); 
  $user_data = get_user_by('id', $logged_in_user);
  if (true === WP_DEBUG) {
   if(empty($action))
    $action = $_SERVER['HTTP_HOST'];
   if (is_array($log) || is_object($log))
    $log = print_r($log,true);
   $logdata = [
    'Datetime' => date("Y-m-d H:i:s"),
    'action' => $action,
    'user' => $user_data->user_login,
    'user_id' => $logged_in_user,
    'type' => $type,
    'userAgent' => $_SERVER['HTTP_USER_AGENT'],
    'userIP' => getIPAddresslog(),
    'logData' => $log,
   ];
    error_log(json_encode($logdata));
  }
 }
}
add_action( 'wp_login', 'wp_debug_log_act');
function wp_debug_log_act(){
 write_log('user has been logged in','user.php','users');
}

For example here adding log after user login

Jagdish Sarma Edited question November 22, 2024
Add a Comment