Thursday, October 8, 2009

Drupal Logging

Since drupal 6, they have replaced watchdog logging with dblog.
We are open sourcing one of our library for you to easily log onto the database log "watchdog" table.



/*
* log to database
* @param string category
* @param string sLog
* @param string sType
* WATCHDOG_EMERG => t('emergency'),
WATCHDOG_ALERT => t('alert'),
WATCHDOG_CRITICAL => t('critical'),
WATCHDOG_ERROR => t('error'),
WATCHDOG_WARNING => t('warning'),
WATCHDOG_NOTICE => t('notice'),
WATCHDOG_INFO => t('info'),
WATCHDOG_DEBUG => t('debug'),
* @param array variables
*/
function logger_log($sCategory, $sLog, $sType="info", $aVars=array())
{
switch ($sType)
{
case "emergency":
$iType = WATCHDOG_EMERG;
break;
case "alert":
$iType = WATCHDOG_ALERT;
break;
case "critical":
$iType = WATCHDOG_CRITICAL;
break;
case "error":
case "err":
$iType = WATCHDOG_ERROR;
break;
case "warning":
case "warn":
$iType = WATCHDOG_WARNING;
break;
case "note":
case "notice":
$iType = WATCHDOG_NOTICE;
break;
case "info":
$iType = WATCHDOG_INFO;
break;
case "debug":
$iType = WATCHDOG_DEBUG;
break;
default:
$iType = 999;//unknown
}

$aLog = array(
"user" => $GLOBALS["user"],
"type" => $sCategory,
"message" => $sLog,
"variables" => $aVars,
"severity" => $iType,
"request_uri" => $_SERVER["REQUEST_URI"],
"referer" => $_SERVER["HTTP_REFERER"],
"ip" => $_SERVER["REMOTE_ADDR"],
"timestamp" => time()
);

dblog_watchdog($aLog);
}

No comments: