Skip to Content
Menu

debug_log()

Append a message to a custom log file.

PHP April 11, 2021

Usage

PHP
nebula()->debug_log($message, $filepath)

Parameters

$message
(Optional) (String) The message to append to the log file
Default: None

$filepath
(Optional) (String) The filepath to the log file
Default: get_stylesheet_directory() . '/nebula_log.log'

Parameter Notes

The message is not technically required, but is strongly recommended.

If no filepath is provided, it will be logged to “nebula_log.log” located in the child theme directory (or theme directory if not using a child theme).

Note: This function will create the file if it does not exist, but it will not create directories if they do not yet exist.

Request or provide clarification »

Examples

This will create a nebula_log.log file in the child theme directory.

PHP
nebula()->debug_log('This is a test')

Write to a custom file within a directory (make sure the directory already exists as this function does not create new directories).

PHP
nebula()->debug_log('This is a test', get_stylesheet_directory() . '/debug/test.log')

Additional Notes

The format of each line in the log is:

[timestamp] message (on file)

Example:

[Saturday, April 19, 2020 – 10:51:51am] This is a test. (on https://nebula.gearside.com/documentation/)

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Logs.php on line 60.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    This function has no filter hooks available. Request one?

    Actions
    "qm/debug"
    Need a new action hook? Request one here.

    PHP
            public function debug_log($message='', $filepath=false){
                if ( empty($filepath) ){
                    $filepath = get_template_directory() . '/nebula.log';
                    if ( is_child_theme() ){
                        $filepath = get_stylesheet_directory() . '/nebula.log'; //Use the child theme directory if using a child theme
                    }
                }
    
                //If the message is not a string, encode it as JSON
                if ( !is_string($message) ){
                    $message = wp_json_encode($message);
                }
    
                $message = '[' . date('l, F j, Y - g:i:sa') . '] ' . $message . ' (on ' . $this->requested_url() . ')' . PHP_EOL; //Add timestamp, URL, and newline
    
                file_put_contents($filepath, $message, FILE_APPEND); //Create the log file if needed and append to it
                do_action('qm/debug', $message); //Log it in Query Monitor as well
            }
    

    Override

    This function can not be short-circuited with an override filter. Request one?