2011-04-20 06:46:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Logging {
|
|
|
|
|
|
|
|
private static $_logger;
|
|
|
|
private static $_path;
|
|
|
|
|
2012-08-24 03:58:21 +02:00
|
|
|
public static function getLogger()
|
2012-08-08 06:08:46 +02:00
|
|
|
{
|
2012-05-06 04:28:52 +02:00
|
|
|
if (!isset(self::$_logger)) {
|
2011-04-20 06:46:03 +02:00
|
|
|
$writer = new Zend_Log_Writer_Stream(self::$_path);
|
2012-05-06 04:28:52 +02:00
|
|
|
|
2012-08-08 06:08:46 +02:00
|
|
|
if (Zend_Version::compareVersion("1.11") > 0) {
|
2012-05-06 04:28:52 +02:00
|
|
|
//Running Zend version 1.10 or lower. Need to instantiate our
|
|
|
|
//own Zend Log class with backported code from 1.11.
|
|
|
|
require_once __DIR__."/AirtimeLog.php";
|
|
|
|
self::$_logger = new Airtime_Zend_Log($writer);
|
|
|
|
} else {
|
|
|
|
self::$_logger = new Zend_Log($writer);
|
|
|
|
}
|
|
|
|
self::$_logger->registerErrorHandler();
|
2011-04-20 06:46:03 +02:00
|
|
|
}
|
|
|
|
return self::$_logger;
|
|
|
|
}
|
|
|
|
|
2012-08-08 06:08:46 +02:00
|
|
|
public static function setLogPath($path)
|
|
|
|
{
|
2011-04-20 06:46:03 +02:00
|
|
|
self::$_path = $path;
|
|
|
|
}
|
2011-08-15 22:40:24 +02:00
|
|
|
|
2012-08-08 06:08:46 +02:00
|
|
|
public static function toString($p_msg)
|
|
|
|
{
|
|
|
|
if (is_array($p_msg) || is_object($p_msg)) {
|
2012-04-13 22:50:54 +02:00
|
|
|
return print_r($p_msg, true);
|
2013-01-30 20:34:06 +01:00
|
|
|
} else if (is_bool($p_msg)) {
|
|
|
|
return $p_msg ? "true" : "false";
|
2012-04-13 22:50:54 +02:00
|
|
|
} else {
|
|
|
|
return $p_msg;
|
|
|
|
}
|
|
|
|
}
|
2015-01-28 00:24:13 +01:00
|
|
|
|
|
|
|
/** @param debugMode Prints the function name, file, and line number. This is slow as it uses debug_backtrace()
|
|
|
|
* so don't use it unless you need it.
|
|
|
|
*/
|
|
|
|
private static function getLinePrefix($debugMode=false)
|
|
|
|
{
|
|
|
|
$linePrefix = "";
|
|
|
|
|
|
|
|
if (array_key_exists('SERVER_NAME', $_SERVER)) {
|
|
|
|
$linePrefix .= $_SERVER['SERVER_NAME'] . " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($debugMode) {
|
|
|
|
//debug_backtrace is SLOW so we don't want this invoke unless there was a real error! (hence $debugMode)
|
|
|
|
$bt = debug_backtrace();
|
|
|
|
$caller = $bt[1];
|
|
|
|
$file = basename($caller['file']);
|
|
|
|
$line = $caller['line'];
|
|
|
|
$function = "Unknown function";
|
|
|
|
if (array_key_exists(2, $bt)) {
|
|
|
|
$function = $bt[2]['function'];
|
|
|
|
}
|
|
|
|
$linePrefix .= "[$file:$line - $function()] - ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $linePrefix;
|
|
|
|
}
|
2012-04-13 22:50:54 +02:00
|
|
|
|
2012-08-22 00:41:56 +02:00
|
|
|
public static function info($p_msg)
|
2012-08-08 06:08:46 +02:00
|
|
|
{
|
2011-08-15 22:40:24 +02:00
|
|
|
$logger = self::getLogger();
|
2015-01-28 00:24:13 +01:00
|
|
|
$logger->info(self::getLinePrefix() . self::toString($p_msg));
|
2011-08-15 22:40:24 +02:00
|
|
|
}
|
2012-08-27 20:51:35 +02:00
|
|
|
|
|
|
|
public static function warn($p_msg)
|
|
|
|
{
|
|
|
|
$logger = self::getLogger();
|
2015-01-28 00:24:13 +01:00
|
|
|
$logger->warn(self::getLinePrefix() . self::toString($p_msg));
|
2012-08-27 20:51:35 +02:00
|
|
|
}
|
2013-04-20 02:11:33 +02:00
|
|
|
|
|
|
|
public static function error($p_msg)
|
|
|
|
{
|
|
|
|
$logger = self::getLogger();
|
2015-01-28 00:24:13 +01:00
|
|
|
$logger->err(self::getLinePrefix(true) . self::toString($p_msg));
|
2013-04-20 02:11:33 +02:00
|
|
|
}
|
2011-12-02 22:22:54 +01:00
|
|
|
|
2012-08-08 06:08:46 +02:00
|
|
|
public static function debug($p_msg)
|
|
|
|
{
|
2012-09-06 18:17:18 +02:00
|
|
|
if (!(defined('APPLICATION_ENV') && APPLICATION_ENV == "development")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$logger = self::getLogger();
|
2015-01-28 00:24:13 +01:00
|
|
|
$logger->debug(self::getLinePrefix(true) . self::toString($p_msg));
|
2012-09-06 18:17:18 +02:00
|
|
|
}
|
|
|
|
// kind of like debug but for printing arrays more compactly (skipping
|
|
|
|
// empty elements
|
|
|
|
|
|
|
|
public static function debug_sparse(array $p_msg)
|
|
|
|
{
|
|
|
|
Logging::debug("Sparse output:");
|
|
|
|
Logging::debug( array_filter($p_msg) );
|
2011-12-02 22:22:54 +01:00
|
|
|
}
|
2012-08-24 04:27:24 +02:00
|
|
|
|
|
|
|
public static function enablePropelLogging()
|
|
|
|
{
|
|
|
|
$logger = Logging::getLogger();
|
|
|
|
Propel::setLogger($logger);
|
|
|
|
|
|
|
|
$con = Propel::getConnection();
|
|
|
|
$con->useDebug(true);
|
|
|
|
|
|
|
|
$config = Propel::getConfiguration(PropelConfiguration::TYPE_OBJECT);
|
|
|
|
$config->setParameter('debugpdo.logging.details.method.enabled', true);
|
|
|
|
$config->setParameter('debugpdo.logging.details.time.enabled', true);
|
|
|
|
$config->setParameter('debugpdo.logging.details.mem.enabled', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function disablePropelLogging()
|
|
|
|
{
|
|
|
|
$con = Propel::getConnection();
|
|
|
|
$con->useDebug(false);
|
|
|
|
Propel::setLogger(null);
|
|
|
|
}
|
|
|
|
|
2011-04-20 06:46:03 +02:00
|
|
|
}
|