miniflux-legacy/vendor/miniflux/picofeed/lib/PicoFeed/Logging/Logger.php

115 lines
1.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace PicoFeed\Logging;
2014-05-20 20:20:27 +02:00
use DateTime;
use DateTimeZone;
/**
* Logging class.
2014-05-20 20:20:27 +02:00
*
* @author Frederic Guillot
*/
class Logger
{
2014-05-20 20:20:27 +02:00
/**
* List of messages.
2014-05-20 20:20:27 +02:00
*
* @static
*
2014-05-20 20:20:27 +02:00
* @var array
*/
private static $messages = array();
/**
* Default timezone.
2014-05-20 20:20:27 +02:00
*
* @static
*
2014-10-19 20:42:31 +02:00
* @var string
2014-05-20 20:20:27 +02:00
*/
private static $timezone = 'UTC';
/**
* Enable or disable logging.
*
* @static
*
* @var bool
*/
public static $enable = false;
/**
* Enable logging.
*
* @static
*/
public static function enable()
{
self::$enable = true;
}
2014-05-20 20:20:27 +02:00
/**
* Add a new message.
2014-05-20 20:20:27 +02:00
*
* @static
*
* @param string $message Message
2014-05-20 20:20:27 +02:00
*/
public static function setMessage($message)
{
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.
2014-05-20 20:20:27 +02:00
*
* @static
*
2014-05-20 20:20:27 +02:00
* @return array
*/
public static function getMessages()
{
return self::$messages;
}
/**
* Remove all logged messages.
2014-05-20 20:20:27 +02:00
*
* @static
*/
public static function deleteMessages()
{
self::$messages = array();
}
2014-05-20 20:20:27 +02:00
/**
* Set a different timezone.
2014-05-20 20:20:27 +02:00
*
* @static
*
2014-05-20 20:20:27 +02:00
* @see http://php.net/manual/en/timezones.php
*
* @param string $timezone Timezone
2014-05-20 20:20:27 +02:00
*/
public static function setTimeZone($timezone)
{
2014-05-20 20:20:27 +02:00
self::$timezone = $timezone ?: self::$timezone;
}
/**
* Get all messages serialized into a string.
*
* @static
*
* @return string
*/
public static function toString()
{
return implode(PHP_EOL, self::$messages).PHP_EOL;
}
2014-05-20 20:20:27 +02:00
}