How To Log Debug Info

Using a log file for debug messages is a good idea, especially if you are running into issues with browser redirections that might cause "too many redirect" errors. Logging messages to a file allows you to capture all relevant debug information without interfering with the user experience.

How to Log Debug Information to a File

You can modify your script to write all debug messages to a log file. Here’s how you can do it:

  1. Create a Function to Handle Debug Logging Create a function that appends debug messages to a log file. You can place this function in your Yoti class or somewhere globally accessible.

    function logDebugMessage($message) {
        $logFile = '/path/to/your/log/file/debug.log'; // Update with the actual path where you want the log file to be
        $timestamp = date('Y-m-d H:i:s');
        $formattedMessage = "[{$timestamp}] {$message}\n";
        file_put_contents($logFile, $formattedMessage, FILE_APPEND);
    }
    
  2. Use This Function in Your Yoti Class Replace the addDebugMessage and echo statements with logDebugMessage calls. This will capture everything in a log file without causing redirection loops or relying on output to the browser.

    Example:

    private function launchYoti($session_id) {
        logDebugMessage("Launching Yoti verification process...");
        $redirect_url = "https://age.yoti.com/?sessionId={$session_id}&sdkId={$this->yoti_SDK_ID}";
        logDebugMessage("Redirecting to Yoti: {$redirect_url}");
        header("Location: {$redirect_url}");
        exit();
    }
    

    Similarly, update other parts of your script:

    private function redirectToContent() {
        $site_version = $_SESSION['user_data']['site_version'] ?? 'tubes';
        $nats_code = $_SESSION['user_data']['nats_code'] ?? 'MTA2MDgzLjYzLjEuMTY1LjAuMC4wLjAuMA';
    
        $redirect_url = "https://{$site_version}.nakedsword.com/?nats={$nats_code}";
    
        logDebugMessage("Redirecting user to content: {$redirect_url}");
        header("Location: {$redirect_url}");
        exit();
    }
    
  3. Log All Important Steps Update each of your important functions with logDebugMessage calls. This way, you will have a complete log of the flow and state of the application, which will help you track where things go wrong.

  4. Accessing the Log File You can then access the log file at /path/to/your/log/file/debug.log to review the messages. Make sure that your log file path is writable by the server. You can use a path like /var/log/your-app/debug.log or another directory where your web server has write permissions.

Example with a Full Debug Log

For instance, if you are checking if a session ID is found:

private function checksessionId() {
    if (isset($_SESSION['yoti_session_id']) && !empty($_SESSION['yoti_session_id'])) {
        logDebugMessage("Yoti session ID found: " . $_SESSION['yoti_session_id']);
        return true;
    } else {
        logDebugMessage("No Yoti session ID found.");
        return false;
    }
}

Permissions Note

Ensure that the directory where the log file resides has appropriate write permissions for your web server user (e.g., www-data for Apache). You might need to run:

chmod 664 /path/to/your/log/file/debug.log
chown www-data:www-data /path/to/your/log/file/debug.log

Using this logging approach will provide a consistent way to debug without affecting user flow or causing redirection errors. It’s especially helpful for environments where user interaction needs to be smooth and uninterrupted, while also needing comprehensive debugging information.

 

Scroll to Top