Skip to content

Commit

Permalink
Merge a07073c into de13eb4
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclay committed Jan 3, 2018
2 parents de13eb4 + a07073c commit be99fc1
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 109 deletions.
6 changes: 6 additions & 0 deletions docs/appendix/upgrade-notes/2.x-to-3.0.rst
Expand Up @@ -646,6 +646,7 @@ Miscellaneous API changes
* ``elgg_list_entities`` no longer supports the option ``view_type_toggle``
* ``elgg_list_registered_entities`` no longer supports the option ``view_type_toggle``
* ``elgg_log`` no longer accepts the level ``"DEBUG"``
* ``elgg_dump`` no longer accepts a ``$to_screen`` argument.
* ``elgg_gatekeeper`` and ``elgg_admin_gatekeeper`` no longer report ``login`` or ``admin`` as forward reason, but ``403``
* ``Application::getDb()`` no longer returns an instance of ``Elgg\Database``, but rather a ``Elgg\Application\Database``
* ``$CONFIG`` is no longer available as a local variable inside plugin ``start.php`` files.
Expand Down Expand Up @@ -973,3 +974,8 @@ System Log

System log API has been moved out of core into a ``system_log`` plugin.
``logbrowser`` and ``logrotate`` plugins have been merged into the ``system_log`` plugin.

Error/info Logging
------------------

Logging to page output is now only done by the developers plugin. Debugging should generally be done via the ``xdebug``` extension or ``tail -f /path/to/error.log`` on your server.
12 changes: 12 additions & 0 deletions elgg-config/settings.example.php
Expand Up @@ -351,3 +351,15 @@
* @global string $CONFIG->image_processor
*/
//$CONFIG->image_processor = 'imagick';

/**
* Logging level
*
* By default, the logging level at boot-time is calculated from PHP's error_reporting(), and during boot
* it is changed to the value specified on the Advanced Settings page. INFO-level events like DB queries
* will not be logged during the initial boot.
*
* However, if the level is set here, it will be used during the entire request. It can be set to one of
* the string levels in Elgg\Logger or ''. E.g., use 'INFO' to log all DB queries during boot up.
*/
//$CONFIG->debug = 'INFO';
7 changes: 3 additions & 4 deletions engine/classes/Elgg/BootService.php
Expand Up @@ -137,10 +137,9 @@ public function boot(ServiceProvider $services) {
$services->dataCache->metadata->save($guid, $metadata);
}

$services->logger->setLevel($config->debug);
if ($config->debug) {
$services->logger->setDisplay(true);
}
// use value in settings.php if available
$debug = $config->hasInitialValue('debug') ? $config->getInitialValue('debug') : $config->debug;
$services->logger->setLevel($debug);

// finish boot sequence
_elgg_session_boot($services);
Expand Down
4 changes: 2 additions & 2 deletions engine/classes/Elgg/Di/ServiceProvider.php
Expand Up @@ -11,7 +11,7 @@
use Elgg\Database\DbConfig;
use Elgg\Database\SiteSecret;
use Elgg\Printer\CliPrinter;
use Elgg\Printer\HtmlPrinter;
use Elgg\Printer\ErrorLogPrinter;
use Elgg\Project\Paths;
use Zend\Mail\Transport\TransportInterface as Mailer;

Expand Down Expand Up @@ -387,7 +387,7 @@ public function __construct(Config $config) {
if (php_sapi_name() === 'cli') {
return new CliPrinter();
} else {
return new HtmlPrinter();
return new ErrorLogPrinter();
}
});

Expand Down
75 changes: 21 additions & 54 deletions engine/classes/Elgg/Logger.php
@@ -1,7 +1,7 @@
<?php
namespace Elgg;

use Elgg\Printer\HtmlPrinter;
use Elgg\Printer\ErrorLogPrinter;

/**
* WARNING: API IN FLUX. DO NOT USE DIRECTLY.
Expand Down Expand Up @@ -40,11 +40,6 @@ class Logger {
*/
protected $level = self::ERROR;

/**
* @var bool Display to user?
*/
protected $display = false;

/**
* @var PluginHooksService
*/
Expand Down Expand Up @@ -82,16 +77,22 @@ public function __construct(PluginHooksService $hooks, Context $context, Config
$this->hooks = $hooks;
$this->context = $context;
if (!isset($printer)) {
$printer = new HtmlPrinter();
$printer = new ErrorLogPrinter();
}
$this->printer = $printer;
$this->config = $config;

$php_error_level = error_reporting();

if (($php_error_level & E_ALL) == E_ALL) {
$this->level = self::INFO;
} elseif (($php_error_level & E_NOTICE) == E_NOTICE) {

// value is in settings.php, use until boot values are available
if ($this->config->hasInitialValue('debug')) {
$this->setLevel($this->config->debug);
return;
}

$this->level = self::OFF;

if (($php_error_level & E_NOTICE) == E_NOTICE) {
$this->level = self::NOTICE;
} elseif (($php_error_level & E_WARNING) == E_WARNING) {
$this->level = self::WARNING;
Expand Down Expand Up @@ -142,20 +143,6 @@ public function getLevel() {
return $this->level;
}

/**
* Set whether the logging should be displayed to the user
*
* Whether data is actually displayed to the user depends on this setting
* and other factors such as whether we are generating a JavaScript or CSS
* file.
*
* @param bool $display Whether to display logging
* @return void
*/
public function setDisplay($display) {
$this->display = $display;
}

/**
* Set custom printer
*
Expand Down Expand Up @@ -199,10 +186,7 @@ public function log($message, $level = self::NOTICE) {

$levelString = self::$levels[$level];

// notices and below never displayed to user
$display = $this->display && $level > self::NOTICE;

$this->process("$levelString: $message", $display, $level);
$this->process("$levelString: $message", $level);

return true;
}
Expand Down Expand Up @@ -248,52 +232,35 @@ public function info($message) {
}

/**
* Dump data to log or screen
* Dump data to log
*
* @param mixed $data The data to log
* @param bool $display Whether to include this in the HTML page
* @param mixed $data The data to log
* @return void
*/
public function dump($data, $display = true) {
$this->process($data, $display, self::ERROR);
public function dump($data) {
$this->process($data, self::ERROR);
}

/**
* Process logging data
*
* @param mixed $data The data to process
* @param bool $display Whether to display the data to the user. Otherwise log it.
* @param int $level The logging level for this data
* @param mixed $data The data to process
* @param int $level The logging level for this data
* @return void
*/
protected function process($data, $display, $level) {
protected function process($data, $level) {

// plugin can return false to stop the default logging method
$params = [
'level' => $level,
'msg' => $data,
'display' => $display,
'to_screen' => $display,
];

if (!$this->hooks->trigger('debug', 'log', $params, true)) {
return;
}

// Do not want to write to JS or CSS pages
if ($this->context->contains('js') || $this->context->contains('css')) {
$display = false;
}

// don't display in simplecache requests
if ($this->config->bootcomplete) {
$path = substr(current_page_url(), strlen(elgg_get_site_url()));
if (preg_match('~^(cache|action|serve-file)/~', $path)) {
$display = false;
}
}

$this->printer->write($data, $display, $level);
$this->printer->write($data, $level);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions engine/classes/Elgg/Printer.php
Expand Up @@ -12,11 +12,10 @@ interface Printer {
/**
* Prints data
*
* @param mixed $data Data to print
* @param bool $display If display is expected by the logger
* @param string $level Logging level
* @param mixed $data Data to print
* @param string $level Logging level
*
* @return mixed
*/
public function write($data, $display, $level);
public function write($data, $level);
}
2 changes: 1 addition & 1 deletion engine/classes/Elgg/Printer/CliPrinter.php
Expand Up @@ -18,7 +18,7 @@ class CliPrinter implements Printer {
/**
* {@inheritdoc}
*/
public function write($data, $display, $level) {
public function write($data, $level) {
if (!is_string($data)) {
VarDumper::dump($data);
return;
Expand Down
20 changes: 20 additions & 0 deletions engine/classes/Elgg/Printer/ErrorLogPrinter.php
@@ -0,0 +1,20 @@
<?php

namespace Elgg\Printer;

use Elgg\Printer;

/**
* Default Log Printer
*
* @access private
*/
class ErrorLogPrinter implements Printer {

/**
* {@inheritdoc}
*/
public function write($data, $level) {
error_log(print_r($data, true));
}
}
26 changes: 0 additions & 26 deletions engine/classes/Elgg/Printer/HtmlPrinter.php

This file was deleted.

21 changes: 8 additions & 13 deletions engine/lib/elgglib.php
Expand Up @@ -814,15 +814,13 @@ function elgg_get_ordered_event_handlers($event, $type) {
}

/**
* Display or log a message.
* Log a message.
*
* If $level is >= to the debug setting in {@link $CONFIG->debug}, the
* message will be sent to {@link elgg_dump()}. Messages with lower
* priority than {@link $CONFIG->debug} are ignored.
*
* Outputs all levels but NOTICE to screen by default.
*
* @note No messages will be displayed unless debugging has been enabled.
* @note Use the developers plugin to display logs
*
* @param string $message User message
* @param string $level NOTICE | WARNING | ERROR
Expand All @@ -847,21 +845,19 @@ function elgg_log($message, $level = 'NOTICE') {
}

/**
* Logs or displays $value.
*
* If $to_screen is true, $value is displayed to screen. Else,
* it is handled by PHP's {@link error_log()} function.
* Logs $value to PHP's {@link error_log()}
*
* A {@elgg_plugin_hook debug log} is called. If a handler returns
* false, it will stop the default logging method.
*
* @param mixed $value The value
* @param bool $to_screen Display to screen?
* @note Use the developers plugin to display logs
*
* @param mixed $value The value
* @return void
* @since 1.7.0
*/
function elgg_dump($value, $to_screen = true) {
_elgg_services()->logger->dump($value, $to_screen);
function elgg_dump($value) {
_elgg_services()->logger->dump($value);
}

/**
Expand Down Expand Up @@ -1384,7 +1380,6 @@ function _elgg_normalize_plural_options_array($options, $singulars) {
*/
function _elgg_shutdown_hook() {
try {
_elgg_services()->logger->setDisplay(false);
elgg_trigger_event('shutdown', 'system');

$time = (float) (microtime(true) - $GLOBALS['START_MICROTIME']);
Expand Down
12 changes: 11 additions & 1 deletion mod/developers/views/default/admin/developers/settings.php
Expand Up @@ -3,6 +3,14 @@
* Developer settings
*/

$config = _elgg_config();
$debug_value = $config->hasInitialValue('debug') ? $config->getInitialValue('debug') : $config->debug;

$debug_help = elgg_echo('developers:help:debug_level');
if ($config->hasInitialValue('debug')) {
$debug_help .= '<br>' . elgg_echo('admin:settings:in_settings_file');
}

$data = [
'simple_cache' => [
'#type' => 'checkbox',
Expand All @@ -28,7 +36,9 @@

'debug_level' => [
'#type' => 'select',
'value' => elgg_get_config('debug'),
'#help' => $debug_help,
'value' => $debug_value,
'disabled' => $config->hasInitialValue('debug'),
'options_values' => [
'' => elgg_echo('developers:debug:off'),
'ERROR' => elgg_echo('developers:debug:error'),
Expand Down
4 changes: 3 additions & 1 deletion mod/developers/views/default/forms/developers/settings.php
Expand Up @@ -23,7 +23,9 @@
$info['#label'] = elgg_echo("developers:label:$name", $echo_vars);
}

$info['#help'] = elgg_echo("developers:help:$name");
if (empty($info['#help'])) {
$info['#help'] = elgg_echo("developers:help:$name");
}
echo elgg_view_field($info);
}

Expand Down
9 changes: 6 additions & 3 deletions views/default/forms/admin/site/advanced/debugging.php
Expand Up @@ -3,8 +3,11 @@
* Advanced site settings, debugging section.
*/

$config = _elgg_config();
$value = $config->hasInitialValue('debug') ? $config->getInitialValue('debug') : $config->debug;

$help = elgg_echo('installation:debug');
if (_elgg_config()->hasInitialValue('debug')) {
if ($config->hasInitialValue('debug')) {
$help .= '<br>' . elgg_echo('admin:settings:in_settings_file');
}

Expand All @@ -20,8 +23,8 @@
'name' => 'debug',
'#label' => elgg_echo('installation:debug:label'),
'#help' => $help,
'value' => elgg_get_config('debug'),
'disabled' => _elgg_config()->hasInitialValue('debug'),
'value' => $value,
'disabled' => $config->hasInitialValue('debug'),
]);

echo elgg_view_module('inline', elgg_echo('admin:legend:debug'), $body, ['id' => 'elgg-settings-advanced-debugging']);

0 comments on commit be99fc1

Please sign in to comment.