Skip to Content
Menu

add_log()

Add an entry to the Nebula administrative log.

PHP May 1, 2020

Usage

PHP
nebula()->add_log($message, $importance)

Parameters

$message
(Required) (String) The message text
Default: None

$importance
(Optional) (Integer) How important this log is (0-10)
Default: None

Parameter Notes

Do not include personally identifiable information in the message. This log database is meant for strictly administrative events– this is not meant to log visitor usage data or personal information!

Logs with an importance of 0 may be automatically removed to save space in the log database.

Note: A timestamp is automatically added to the message as well as the user who logged the message (so those are not needed in the message string).

Request or provide clarification »

Additional Notes

This log option is disabled by default! It will not log anything when disabled, so retroactive logs are not possible. It is available in Nebula Options in the Administrative tab and logs themselves can be viewed in Nebula Options in the Diagnostics tab.

Some examples of administrative events that are logged by Nebula itself:

  • Nebula theme activation
  • Nebula theme updates (via WordPress updater)
  • WordPress core updates (via WordPress updater)
  • Nebula Options saved
  • Admin user registration
  • Automatic Sass disabling
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 121.

    2 Hooks

    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/error"
    "qm/info"
    Need a new action hook? Request one here.

    Note: This function contains 1 to-do comment.

    PHP
            public function add_log($message='', $importance=0, $optimize=true){
                if ( $this->get_option('administrative_log') && is_user_logged_in() && !empty($message) ){
                    global $wpdb;
    
                    try {
                        $log_insertion = $wpdb->insert($wpdb->nebula_logs, array(
                            'timestamp' => sanitize_text_field(date('U')),
                            'message' => sanitize_text_field($message),
                            'user_id' => intval(get_current_user_id()), //Note: returns 0 in cron jobs
                            'importance' => intval($importance)
                        )); //DB Query
                    } catch(Exception $error){
                        //This could happen if the option was enabled (somehow) by non-staff, and a log was attempted to be added
                        $this->update_option('administrative_log', 0); //Disable the option just to be safe. Unfortunately this cannot be logged somewhere...
                        do_action('qm/error', $error);
                        return false;
                    }
    
                    delete_transient('nebula_logs');
                    do_action('qm/info', 'Added Nebula Log: ' . sanitize_text_field($message));
    
    /*
                    if ( !empty($optimize) ){
                        //$this->optimize_logs(); //@todo "nebula" 0: Need to test this before enabling!
                    }
    */
    
                    if ( $this->is_debug(false) || WP_DEBUG || WP_DEBUG_LOG ){
                        $this->debug_log($message . ' [User: ' . get_userdata(intval(get_current_user_id()))->display_name . ']'); //Log the message to a file too when debug mode is active
                    }
    
                    return is_int($log_insertion); //Boolean return
                }
    
                return false;
            }
    

    Override

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