Current Location: Home> Latest Articles> How to debug using error_log() in init function

How to debug using error_log() in init function

gitbox 2025-05-28

In PHP development, sometimes we need to debug during the initialization phase of the program (usually in the init function) to ensure everything runs as expected. At this time, error_log() is a very practical tool that can output debugging information to the server's error log to help us quickly locate problems.

This article will introduce how to use error_log() to debug in init function and give actual code examples.

1?? What is error_log()?

error_log() is a built-in function in PHP that writes specified error information to the server's error log, specified file, or sends it directly to mail. Its basic usage is as follows:

 error_log(string $message [, int $message_type = 0 [, string $destination [, string $extra_headers]]]) : bool

The most commonly used scenario is to simply write debug information to the server log:

 error_log('Debugging information:variable x The value of ' . $x);

2?? Use scenarios in the init function

Suppose you have a system or framework that calls the init function for each request to initialize the configuration, load the library file, or perform checks. At this stage, if an error occurs, the entire system may not work properly, so it is very important to add debug information in the init stage.

3?? Code Example

Here is an example showing how to debug using error_log() in an init function:

 <?php
function init() {
    // Simulate loading configuration
    $config = load_config();

    // Debug output configuration array
    error_log('debug:Loaded configuration = ' . print_r($config, true));

    // Simulate database connections
    $db = db_connect($config['db']);

    // Check if the database connection is successful
    if (!$db) {
        error_log('mistake:Database connection failed');
    } else {
        error_log('debug:Database connection is successful');
    }

    // Simulate loading external resources
    $apiUrl = 'https://api.gitbox.net/resource';
    $response = file_get_contents($apiUrl);

    if ($response === false) {
        error_log('mistake:Can&#39;t get from ' . $apiUrl . ' Get resources');
    } else {
        error_log('debug:Successfully obtain external resources,Response length = ' . strlen($response));
    }
}

function load_config() {
    return [
        'db' => [
            'host' => 'localhost',
            'user' => 'root',
            'pass' => '',
            'name' => 'testdb'
        ]
    ];
}

function db_connect($dbConfig) {
    // This is just a simulation,In practice you might use mysqli or PDO
    if ($dbConfig['host'] === 'localhost') {
        return true; // Simulation is successful
    }
    return false; // Simulation failed
}

// Call init function
init();
?>

4?? Viewing method of debug output

To view the information output from error_log() , you can:

? Check the server's error log file (such as Apache's error.log or PHP's error_log ).
? Make sure error_log and log_errors are set correctly in php.ini .
? If in a development environment, the output file of error_log can be temporarily set, for example:

 ini_set('error_log', '/tmp/my_php_debug.log');