Skip to content

Instantly share code, notes, and snippets.

@rdeutz
Created December 19, 2013 14:08
Show Gist options
  • Save rdeutz/8039614 to your computer and use it in GitHub Desktop.
Save rdeutz/8039614 to your computer and use it in GitHub Desktop.
two functions to log $data into a file using JLog
protected function logData($data, $type="ERROR")
{
jimport('joomla.log.log');
$types = array(
'EMERGENCY',
'ALERT',
'CRITICAL',
'ERROR',
'WARNING',
'NOTICE',
'INFO',
'DEBUG'
);
if (!in_array($type, $types))
{
$type = 'EMERGENCY';
}
$loglevel = constant('JLog::' . $type);
JLog::addLogger(
array(
//Sets file name
'text_file' => 'com_babioonad.'.strtolower($type).'.php'
),
$loglevel,
//Chooses a category name
'com_babioonad'
);
JLog::add('- START LOGGING DATA -', $loglevel, 'com_babioonad');
// we are trying to convert the data to an array
$convertedData = array();
if (is_array($data))
{
$convertedData = $data;
}
else
{
if (is_string($data) || is_bool($data) || is_integer($data) || is_float($data))
{
$convertedData = (array) $data;
}
else
{
if (is_object($data))
{
$convertedData = JArrayHelper::fromObject($data);
}
}
}
$this->doLog($convertedData, $loglevel);
JLog::add('- END LOGGING DATA -', $loglevel, 'com_babioonad');
}
private function doLog($data, $loglevel, $level=0, $component='com_babioonad')
{
foreach ($data as $key => $value)
{
if (is_array($value))
{
if ($level < 50)
{
$this->doLog($value, $loglevel, $level++);
}
}
else
{
JLog::add($key . '=' . $value, $loglevel, 'com_babioonad');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment