2013-07-06 20:29:45 +02:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoFeed\Logging;
|
2013-07-06 20:29:45 +02:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logging class
|
|
|
|
*
|
|
|
|
* @author Frederic Guillot
|
2014-12-24 03:28:26 +01:00
|
|
|
* @package Logging
|
2014-05-20 20:20:27 +02:00
|
|
|
*/
|
2014-12-24 03:28:26 +01:00
|
|
|
class Logger
|
2013-07-06 20:29:45 +02:00
|
|
|
{
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* List of messages
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $messages = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default timezone
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access private
|
2014-10-19 20:42:31 +02:00
|
|
|
* @var string
|
2014-05-20 20:20:27 +02:00
|
|
|
*/
|
|
|
|
private static $timezone = 'UTC';
|
|
|
|
|
2015-01-20 02:00:16 +01:00
|
|
|
/**
|
|
|
|
* Enable or disable logging
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access public
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public static $enable = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable logging
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public static function enable()
|
|
|
|
{
|
|
|
|
self::$enable = true;
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Add a new message
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access public
|
|
|
|
* @param string $message Message
|
|
|
|
*/
|
|
|
|
public static function setMessage($message)
|
|
|
|
{
|
2015-01-20 02:00:16 +01:00
|
|
|
if (self::$enable) {
|
|
|
|
$date = new DateTime('now', new DateTimeZone(self::$timezone));
|
|
|
|
self::$messages[] = '['.$date->format('Y-m-d H:i:s').'] '.$message;
|
|
|
|
}
|
2014-05-20 20:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all logged messages
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getMessages()
|
|
|
|
{
|
|
|
|
return self::$messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all logged messages
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public static function deleteMessages()
|
|
|
|
{
|
|
|
|
self::$messages = array();
|
|
|
|
}
|
2013-07-06 20:29:45 +02:00
|
|
|
|
2014-05-20 20:20:27 +02:00
|
|
|
/**
|
|
|
|
* Set a different timezone
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @see http://php.net/manual/en/timezones.php
|
|
|
|
* @access public
|
|
|
|
* @param string $timezone Timezone
|
|
|
|
*/
|
|
|
|
public static function setTimeZone($timezone)
|
2013-07-06 20:29:45 +02:00
|
|
|
{
|
2014-05-20 20:20:27 +02:00
|
|
|
self::$timezone = $timezone ?: self::$timezone;
|
2013-07-06 20:29:45 +02:00
|
|
|
}
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all messages serialized into a string
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function toString()
|
|
|
|
{
|
|
|
|
return implode(PHP_EOL, self::$messages).PHP_EOL;
|
|
|
|
}
|
2014-05-20 20:20:27 +02:00
|
|
|
}
|