Debug.log is more useful than you’d imagine. It is not just meant for error logging when WordPress site actually triggers errors.

By creating a custom logging function, you can output any debug information to debug.log dynamically, whether from a theme or from a plugin.

To get this custom logging function to work, first you need to enable debug mode.

Insert the following function code into your functions.php:

if (!function_exists('write_log')) {
    function write_log ( $log )  {
        if ( true === WP_DEBUG ) {
            if ( is_array( $log ) || is_object( $log ) ) {
                error_log( print_r( $log, true ) );
            } else {
                error_log( $log );
            }
        }
    }
}

You can now use write_log function to write anything (text, variable and class instance) to debug.log.

Example:

write_log('Hello World');