sintonia/airtime_mvc/application/logging/Logging.php
Lucas Bickel 625f92fe44 Vendorize ZF1, fix PHPUnit and configure travis
This a a rather large commit due to the nature of the stuff it is touching. To get PHPUnit up and running again I had to update some deps and I did so by vendorizing them. The vendorizing of zf1 makes sense since distros are already considering to drop it from their repos.

* [x] install vendorized zf1 with composer
* [x] load composer autoloader before zf1
* [x] Implement headAction for all Zend_Rest_Controller based controllers
* [x] switch to yml dataset to get around string only limitations of xml sets (also removed warning in readme)
* [x] use year 2044 as hardcoded date for tests since it is in the future and has the same days like previously used 2016
* [x] make tests easier to run when accessing phpunit directly
* [x] clean up test helper to always use airtime.conf
* [x] switch test dbname to libretime_test
* [x] test db username password switched to libretime/libretime
* [x] install phpunit with composer in a clear version (make tests easier to reproduce on other platforms)
* [x] remove local libs from airtime repo (most of airtime_mvc/library was not needed of in vendor already)
* [x] configure composer autoloading and use it (also removed requires that are not needed anymore)
* [x] add LibreTime prefix for FileNotFoundException (phing had a similar class and these are all pre-namespace style)
* [x] add .travis.yml file
* [x] make etc and logdir configurable with LIBRETIME_CONF_DIR and LIBRETIME_LOG_DIR env (so travis can change it)
* [x] slight cleanup in config for travis not to fail
* [x] add cloud_storage.conf for during test runs
* [x] rewrite mvc testing docs and move them to docs/ folder
* [x] don't use `static::class` in a class that does not have a parent class, use `__CLASS__` instead.
* [x] don't use `<ClassName>::class`, since we already know what class we want `"<ClassName>"` ist just fine.
* [x] fix "can't use method in write context" errors on 5.4 (also helps the optimizer)
* [x] add build status badge on main README.md

Fixes https://github.com/LibreTime/libretime/issues/4

The PHP parts of https://github.com/LibreTime/libretime/pull/10 get obsoleted by this change and it will need rebasing.

This also contains https://github.com/LibreTime/libretime/pull/8, the late static binding compat code was broken for no reason and until CentOS drops php 5.4 there is no reason I'm aware of not to support it. I inlined #8 since the test would be failing on php 5.4 without the change.

If you want to run tests you need to run `composer install` in the root directory and then `cd airtime_mvc/tests && ../../vendor/bin/phpunit`. For the tests to run the user `libretime` needs to be allowed to create the `libretime_test` database. See `docs/TESTING.md` for more info on getting set up.
2017-02-27 17:59:01 +01:00

178 lines
5.5 KiB
PHP

<?php
class Logging {
private static $_logger;
private static $_path;
public static function getLogger()
{
if (!isset(self::$_logger)) {
$writer = new Zend_Log_Writer_Stream(self::$_path);
if (Zend_Version::compareVersion("1.11") > 0) {
//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();
}
return self::$_logger;
}
public static function setLogPath($path)
{
self::$_path = $path;
}
public static function toString($p_msg)
{
if (is_array($p_msg) || is_object($p_msg)) {
return print_r($p_msg, true);
} else if (is_bool($p_msg)) {
return $p_msg ? "true" : "false";
} else {
return $p_msg;
}
}
/** @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;
}
public static function info($p_msg)
{
$logger = self::getLogger();
$logger->info(self::getLinePrefix() . self::toString($p_msg));
}
public static function warn($p_msg)
{
$logger = self::getLogger();
$logger->warn(self::getLinePrefix() . self::toString($p_msg));
}
public static function error($p_msg)
{
$logger = self::getLogger();
$logger->err(self::getLinePrefix(true) . self::toString($p_msg));
//Escape the % symbols in any of our errors because Sentry chokes (vsprint formatting error).
$msg = self::toString($p_msg);
$msg = str_replace("%", "%%", $msg);
SentryLogger::getInstance()->captureError($msg);
}
public static function debug($p_msg)
{
if (!(defined('APPLICATION_ENV') && APPLICATION_ENV == "development")) {
return;
}
$logger = self::getLogger();
$logger->debug(self::getLinePrefix(true) . self::toString($p_msg));
}
// 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) );
}
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);
}
public static function loggingShutdownCallback()
{
//Catch the types of errors that PHP doesn't normally let us catch and
//would otherwise log to the apache log. We route these to our Airtime log to improve the modularity
//and reliability of our error logging. (All errors are in one spot!)
$err = error_get_last();
if (!is_array($err) || !array_key_exists('type', $err)) {
return;
}
switch($err['type'])
{
case E_ERROR:
case E_WARNING:
case E_PARSE:
case E_NOTICE:
case E_CORE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_ERROR:
case E_COMPILE_WARNING:
//error_log("Oh noes, a fatal: " . var_export($err, true), 1, 'fatals@example.com');
$errorStr = '';
if (array_key_exists('message', $err)) {
$errorStr .= $err['message'];
}
if (array_key_exists('file', $err))
{
$errorStr .= ' at ' .$err['file'];
}
if (array_key_exists('line', $err))
{
$errorStr .= ':' . $err['line'];
}
$errorStr .= "\n" . var_export($err, true);
Logging::error($errorStr);
break;
}
}
public static function setupParseErrorLogging()
{
//Static callback:
register_shutdown_function('Logging::loggingShutdownCallback');
}
}