Skip to Content

debug_log()

Append a message to a custom log file.

PHP April 24, 2020

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/)

Source File

Located in /libs/Utilities/Utilities.php on line 656.

No Hooks

This function does not have any filters or actions available. Request one?
PHP
        public function debug_log($message='', $filepath=false){
            if ( empty($filepath) ){

                $filepath = get_template_directory() . '/nebula_log.log';
                if ( is_child_theme() ){
                    $filepath = get_stylesheet_directory() . '/nebula_log.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 = 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
        }

Override

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