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 102.

    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, $verbose=false, $limited=true){
                if ( $this->is_minimal_mode() ){return null;}
    
                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
                    }
                }
    
                //Ensure the directory for the file exists
                if ( !file_exists(dirname($filepath)) ){
                    wp_mkdir_p(dirname($filepath)); //Create the directory and any necessary parents
                }
    
                //If the message is not a string, encode it as JSON
                if ( !is_string($message) ){
                    $message = wp_json_encode($message);
                }
    
                //If verbose data is requested, add it to the message
                if ( !empty($verbose) ){
                    $message .= ' | IP: ' . $this->get_ip_address(false);
                    $message .= ' | Role: ' . $this->user_role();
                    $message .= ' | SID: ' . $this->nebula_session_id();
    
                    if ( !empty($this->super->server['HTTP_USER_AGENT']) ){
                        $message .= ' | UA: ' . $this->super->server['HTTP_USER_AGENT'];
                    }
                }
    
                $message = '[' . date('l, F j, Y g:i:sa') . '] ' . $message . ' | URL: ' . $this->requested_url() . PHP_EOL; //Add timestamp, URL, and newline
    
                //Limit the file size of Nebula-based log files
                if ( !empty($limited) ){
                    if ( file_exists($filepath) && filesize($filepath) > (MB_IN_BYTES*100) ){
                        $lines = explode('\n', file_get_contents($filepath));
                        $half = array_slice($lines,ceil(count($lines)/2)); //Keep last 50%
                        file_put_contents($filepath, implode('\n', $half));
                    }
                }
    
                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?