Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Created April 4, 2017 02:58
Show Gist options
  • Save molotovbliss/6ca979a194e6060b140408d876823296 to your computer and use it in GitHub Desktop.
Save molotovbliss/6ca979a194e6060b140408d876823296 to your computer and use it in GitHub Desktop.
Magento 2.x PHP Bootstrap file with Logging
<?php
// B00MER / mb@molotovbliss.com
// ============================================================================
// Adding Logging ability from start with Mage::Log no longer available
// globally + a few PHP specific values. Feel free to re-use as needed!
// Quick dirty PHP bootstrapped with framework. Technically you should use
// the Dev Test area with Unit Testing: /dev/tests/test.php instead
// ============================================================================
// Set basic PHP settings for development
ini_set('display_errors', 1);
ini_set('memory_limit','10242M');
error_reporting(E_ALL | E_STRICT);
set_time_limit(0);
umask (0);
// Set developer mode
define('MAGE_MODE', 'developer');
$_SERVER['MAGE_MODE'] = MAGE_MODE;
// This dev test file meant to sit in the root magento folder
define('MAGENTO_ROOT', getcwd()); // adjust accordingly if elsewhere.
require MAGENTO_ROOT . '/app/bootstrap.php';
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// ============================================================================
// Basic Framework initialize, get objects, state of app,
// define front-end & load a quote from checkout session.
// ============================================================================
// Init bootstrap
$bootstrap = Bootstrap::create(BP, $_SERVER);
// Object Manager & State of App
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
// Assume front-end, get the quote id 1
$state->setAreaCode('frontend');
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote()->load(1);
// ============================================================================
// Creating a log entry since Mage::Log() is not available anywhere globally.
// Make note of the MonoLog class combined with File system class.
// ============================================================================
// Create log file to debug output (/var/log/thisfilename.log)
define('DS', DIRECTORY_SEPARATOR);
define('CURRENT_PHP_FILENAME', pathinfo(__FILE__, PATHINFO_FILENAME));
$logFile = DirectoryList::VAR_DIR . DS . DirectoryList::LOG . DS .
CURRENT_PHP_FILENAME . '.log';
// create a log stream handler
$log = new Logger('log');
$log->pushHandler(new StreamHandler($logFile, Logger::DEBUG));
// log debug output $log/$bootstrap methods & display Quote Id 1 to screen
$log->debug(print_r(get_class_methods($log),true));
$log->debug(var_export(get_class_methods($bootstrap),true));
echo "Is Developer Mode Set? ";
echo $bootstrap->isDeveloperMode() ? 'true' : 'false';
echo PHP_EOL;
print_r($quote->getOrigData());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment