Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

JLog Exception, Class Name capitalization, and DBO #1159

Merged
merged 1 commit into from Apr 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions libraries/import.legacy.php
Expand Up @@ -64,3 +64,5 @@ class_exists('JLoader') or die;
JLoader::register('JSimpleCrypt', JPATH_PLATFORM . '/legacy/simplecrypt/simplecrypt.php');
JLoader::register('JTree', JPATH_PLATFORM . '/legacy/base/tree.php');
JLoader::register('JNode', JPATH_PLATFORM . '/legacy/base/node.php');

JLoader::register('LogException', JPATH_PLATFORM . '/legacy/log/logexception.php');
2 changes: 1 addition & 1 deletion libraries/joomla/log/entry.php
Expand Up @@ -51,7 +51,7 @@ class JLogEntry
public $priority = JLog::INFO;

/**
* List of available log priority levels [Based on the SysLog default levels].
* List of available log priority levels [Based on the Syslog default levels].
* @var array
* @since 11.1
*/
Expand Down
12 changes: 5 additions & 7 deletions libraries/joomla/log/log.php
Expand Up @@ -11,8 +11,6 @@

jimport('joomla.log.logger');

JLoader::register('LogException', JPATH_PLATFORM . '/joomla/log/logexception.php');

JLoader::discover('JLogger', __DIR__ . '/loggers');

// @deprecated 12.1
Expand All @@ -23,8 +21,8 @@
*
* This class hooks into the global log configuration settings to allow for user configured
* logging events to be sent to where the user wishes them to be sent. On high load sites
* SysLog is probably the best (pure PHP function), then the text file based loggers (CSV, W3C
* or plain FormattedText) and finally MySQL offers the most features (e.g. rapid searching)
* Syslog is probably the best (pure PHP function), then the text file based loggers (CSV, W3c
* or plain Formattedtext) and finally MySQL offers the most features (e.g. rapid searching)
* but will incur a performance hit due to INSERT being issued.
*
* @package Joomla.Platform
Expand Down Expand Up @@ -293,7 +291,7 @@ public static function setInstance($instance)
*
* @param array $entry Array of values to map to the format string for the log file.
*
* @return boolean True on success.
* @return mixed null|boolean
*
* @since 11.1
*
Expand Down Expand Up @@ -352,7 +350,7 @@ public function addEntry($entry)
* @return void
*
* @since 11.1
* @throws LogException
* @throws RuntimeException
*/
protected function addLogEntry(JLogEntry $entry)
{
Expand All @@ -372,7 +370,7 @@ protected function addLogEntry(JLogEntry $entry)
}
else
{
throw new LogException(JText::_('Unable to create a JLogger instance: '));
throw new RuntimeException('Unable to create a JLogger instance: ' . $class);
}
}

Expand Down
14 changes: 14 additions & 0 deletions libraries/joomla/log/logger.php
Expand Up @@ -28,6 +28,20 @@ abstract class JLogger
*/
protected $options = array();

/**
* @var array Translation array for JLogEntry priorities to text strings.
* @since 11.1
*/
protected $priorities = array(
JLog::EMERGENCY => 'EMERGENCY',
JLog::ALERT => 'ALERT',
JLog::CRITICAL => 'CRITICAL',
JLog::ERROR => 'ERROR',
JLog::WARNING => 'WARNING',
JLog::NOTICE => 'NOTICE',
JLog::INFO => 'INFO',
JLog::DEBUG => 'DEBUG');

/**
* Constructor.
*
Expand Down
21 changes: 10 additions & 11 deletions libraries/joomla/log/loggers/database.php
Expand Up @@ -15,7 +15,7 @@
* Joomla! MySQL Database Log class
*
* This class is designed to output logs to a specific MySQL database table. Fields in this
* table are based on the SysLog style of log output. This is designed to allow quick and
* table are based on the Syslog style of log output. This is designed to allow quick and
* easy searching.
*
* @package Joomla.Platform
Expand Down Expand Up @@ -72,27 +72,26 @@ class JLoggerDatabase extends JLogger
* @param array &$options Log object options.
*
* @since 11.1
* @throws LogException
*/
public function __construct(array &$options)
{
// Call the parent constructor.
parent::__construct($options);

// If both the database object and driver options are empty we want to use the system database connection.
if (empty($this->options['db_object']) && empty($this->options['db_driver']))
if (empty($this->options['db_driver']))
{
$this->dbo = JFactory::getDBO();
$this->driver = JFactory::getConfig()->get('dbtype');
$this->host = JFactory::getConfig()->get('host');
$this->user = JFactory::getConfig()->get('user');
$this->password = JFactory::getConfig()->get('password');
$this->database = JFactory::getConfig()->get('db');
$this->prefix = JFactory::getConfig()->get('dbprefix');
$this->driver = null;
$this->host = null;
$this->user = null;
$this->password = null;
$this->database = null;
$this->prefix = null;
}
// We need to get the database connection settings from the configuration options.
else
{
$this->dbo = null;
$this->driver = (empty($this->options['db_driver'])) ? 'mysql' : $this->options['db_driver'];
$this->host = (empty($this->options['db_host'])) ? '127.0.0.1' : $this->options['db_host'];
$this->user = (empty($this->options['db_user'])) ? 'root' : $this->options['db_user'];
Expand Down Expand Up @@ -123,7 +122,7 @@ public function addEntry(JLogEntry $entry)
}

// Convert the date.
$entry->date = $entry->date->toSql();
$entry->date = $entry->date->toSql(false, $this->dbo);

$this->dbo->insertObject($this->table, $entry);
}
Expand Down
33 changes: 22 additions & 11 deletions libraries/joomla/log/loggers/echo.php
Expand Up @@ -21,18 +21,27 @@
class JLoggerEcho extends JLogger
{
/**
* @var array Translation array for JLogEntry priorities to text strings.
* @var string Value to use at the end of an echoed log entry to separate lines.
* @since 11.1
*/
protected $priorities = array(
JLog::EMERGENCY => 'EMERGENCY',
JLog::ALERT => 'ALERT',
JLog::CRITICAL => 'CRITICAL',
JLog::ERROR => 'ERROR',
JLog::WARNING => 'WARNING',
JLog::NOTICE => 'NOTICE',
JLog::INFO => 'INFO',
JLog::DEBUG => 'DEBUG');
protected $line_separator = "\n";

/**
* Constructor.
*
* @param array &$options Log object options.
*
* @since 12.1
*/
public function __construct(array &$options)
{
parent::__construct($options);

if (!empty($this->options['line_separator']))
{
$this->line_separator = $this->options['line_separator'];
}
}

/**
* Method to add an entry to the log.
Expand All @@ -45,6 +54,8 @@ class JLoggerEcho extends JLogger
*/
public function addEntry(JLogEntry $entry)
{
echo $this->priorities[$entry->priority] . ': ' . $entry->message . (empty($entry->category) ? '' : ' [' . $entry->category . ']') . "\n";
echo $this->priorities[$entry->priority] . ': '
. $entry->message . (empty($entry->category) ? '' : ' [' . $entry->category . ']')
. $this->line_separator;
}
}
26 changes: 6 additions & 20 deletions libraries/joomla/log/loggers/formattedtext.php
Expand Up @@ -16,13 +16,13 @@
* Joomla! Formatted Text File Log class
*
* This class is designed to use as a base for building formatted text files for output. By
* default it emulates the SysLog style format output. This is a disk based output format.
* default it emulates the Syslog style format output. This is a disk based output format.
*
* @package Joomla.Platform
* @subpackage Log
* @since 11.1
*/
class JLoggerFormattedText extends JLogger
class JLoggerFormattedtext extends JLogger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really try to make these autoloadable, good first step thou.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would we do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to rename them to something like JLogLoggerFormattedtext etc., but lets do that in a later pull.

{
/**
* @var resource The file pointer for the log file.
Expand All @@ -49,20 +49,6 @@ class JLoggerFormattedText extends JLogger
*/
protected $path;

/**
* @var array Translation array for JLogEntry priorities to text strings.
* @since 11.1
*/
protected $priorities = array(
JLog::EMERGENCY => 'EMERGENCY',
JLog::ALERT => 'ALERT',
JLog::CRITICAL => 'CRITICAL',
JLog::ERROR => 'ERROR',
JLog::WARNING => 'WARNING',
JLog::NOTICE => 'NOTICE',
JLog::INFO => 'INFO',
JLog::DEBUG => 'DEBUG');

/**
* Constructor.
*
Expand Down Expand Up @@ -127,7 +113,7 @@ public function __destruct()
* @return boolean True on success.
*
* @since 11.1
* @throws LogException
* @throws RuntimeException
*/
public function addEntry(JLogEntry $entry)
{
Expand Down Expand Up @@ -182,7 +168,7 @@ public function addEntry(JLogEntry $entry)
// Write the new entry to the file.
if (!fputs($this->file, $line . "\n"))
{
throw new LogException;
throw new RuntimeException('Cannot write to log file.');
}
}

Expand Down Expand Up @@ -247,13 +233,13 @@ protected function initFile()
// Open the file for writing (append mode).
if (!$this->file = fopen($this->path, 'a'))
{
// Throw exception.
throw new RuntimeException('Cannot open file for writing log');
}
if ($head)
{
if (!fputs($this->file, $head))
{
throw new LogException;
throw new RuntimeException('Cannot fput file for log');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/joomla/log/loggers/messagequeue.php
Expand Up @@ -15,7 +15,7 @@
* Joomla MessageQueue logger class.
*
* This class is designed to output logs to a specific MySQL database table. Fields in this
* table are based on the SysLog style of log output. This is designed to allow quick and
* table are based on the Syslog style of log output. This is designed to allow quick and
* easy searching.
*
* @package Joomla.Platform
Expand Down
22 changes: 11 additions & 11 deletions libraries/joomla/log/loggers/syslog.php
Expand Up @@ -12,9 +12,9 @@
jimport('joomla.log.logger');

/**
* Joomla! SysLog Log class
* Joomla! Syslog Log class
*
* This class is designed to call the PHP SysLog function call which is then sent to the
* This class is designed to call the PHP Syslog function call which is then sent to the
* system wide log system. For Linux/Unix based systems this is the syslog subsystem, for
* the Windows based implementations this can be found in the Event Log. For Windows,
* permissions may prevent PHP from properly outputting messages.
Expand All @@ -23,11 +23,11 @@
* @subpackage Log
* @since 11.1
*/
class JLoggerSysLog extends JLogger
class JLoggerSyslog extends JLogger
{
/**
* @var array Translation array for JLogEntry priorities to SysLog priority names.
* @since 11.1
* @var array Translation array for JLogEntry priorities to SysLog priority names.
* @since 11.1
*/
protected $priorities = array(
JLog::EMERGENCY => 'EMERG',
Expand All @@ -51,13 +51,13 @@ public function __construct(array &$options)
// Call the parent constructor.
parent::__construct($options);

// Ensure that we have an identity string for the SysLog entries.
// Ensure that we have an identity string for the Syslog entries.
if (empty($this->options['sys_ident']))
{
$this->options['sys_ident'] = 'Joomla Platform';
}

// If the option to add the process id to SysLog entries is set use it, otherwise default to true.
// If the option to add the process id to Syslog entries is set use it, otherwise default to true.
if (isset($this->options['sys_add_pid']))
{
$this->options['sys_add_pid'] = (bool) $this->options['sys_add_pid'];
Expand All @@ -67,7 +67,7 @@ public function __construct(array &$options)
$this->options['sys_add_pid'] = true;
}

// If the option to also send SysLog entries to STDERR is set use it, otherwise default to false.
// If the option to also send Syslog entries to STDERR is set use it, otherwise default to false.
if (isset($this->options['sys_use_stderr']))
{
$this->options['sys_use_stderr'] = (bool) $this->options['sys_use_stderr'];
Expand All @@ -77,7 +77,7 @@ public function __construct(array &$options)
$this->options['sys_use_stderr'] = false;
}

// Build the SysLog options from our log object options.
// Build the Syslog options from our log object options.
$sysOptions = 0;
if ($this->options['sys_add_pid'])
{
Expand All @@ -88,7 +88,7 @@ public function __construct(array &$options)
$sysOptions = $sysOptions | LOG_PERROR;
}

// Open the SysLog connection.
// Open the Syslog connection.
openlog((string) $this->options['sys_ident'], $sysOptions, LOG_USER);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ public function addEntry(JLogEntry $entry)
// Generate the value for the priority based on predefined constants.
$priority = constant(strtoupper('LOG_' . $this->priorities[$entry->priority]));

// Send the entry to SysLog.
// Send the entry to Syslog.
syslog($priority, '[' . $entry->category . '] ' . $entry->message);
}
}
10 changes: 5 additions & 5 deletions libraries/joomla/log/loggers/w3c.php
Expand Up @@ -11,20 +11,20 @@

jimport('joomla.log.logger');

// Register the JLoggerFormattedText class with the autoloader.
JLoader::register('JLoggerFormattedText', __DIR__ . '/formattedtext.php');
// Register the JLoggerFormattedtext class with the autoloader.
JLoader::register('JLoggerFormattedtext', __DIR__ . '/formattedtext.php');

/**
* Joomla! W3C Logging class
* Joomla! W3c Logging class
*
* This class is designed to build log files based on the W3C specification
* This class is designed to build log files based on the W3c specification
* at: http://www.w3.org/TR/WD-logfile.html
*
* @package Joomla.Platform
* @subpackage Log
* @since 11.1
*/
class JLoggerW3C extends JLoggerFormattedText
class JLoggerW3c extends JLoggerFormattedtext
{
/**
* @var string The format which each entry follows in the log file. All fields must be
Expand Down
5 changes: 2 additions & 3 deletions tests/suites/unit/joomla/log/JLogTest.php
Expand Up @@ -9,7 +9,6 @@

require_once JPATH_PLATFORM.'/joomla/log/log.php';
require_once JPATH_PLATFORM.'/joomla/log/entry.php';
require_once JPATH_PLATFORM.'/joomla/log/logexception.php';
require_once JPATH_PLATFORM.'/joomla/log/logger.php';
require_once __DIR__.'/stubs/log/inspector.php';

Expand Down Expand Up @@ -158,8 +157,8 @@ public function testAddLoggerAutoInstantiation()
*/
public function testAddLoggerAutoInstantiationInvalidLogger()
{
// We are expecting a LogException to be thrown since we are trying to add a bogus logger.
$this->setExpectedException('LogException');
// We are expecting a InvalidArgumentException to be thrown since we are trying to add a bogus logger.
$this->setExpectedException('RuntimeException');

JLog::setInstance(null);

Expand Down