| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Util; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use ImportWP\Common\Filesystem\Filesystem; |
| 7 |
use ImportWP\Common\Importer\ImporterManager; |
| 8 |
use ImportWP\Container; |
| 9 |
|
| 10 |
class Logger |
| 11 |
{ |
| 12 |
private static $id = null; |
| 13 |
private static $requestType = null; |
| 14 |
private static $time = -1; |
| 15 |
private static $disabled = false; |
| 16 |
|
| 17 |
public static function setId($id = null) |
| 18 |
{ |
| 19 |
self::$id = $id; |
| 20 |
} |
| 21 |
|
| 22 |
public static function disable() |
| 23 |
{ |
| 24 |
self::$disabled = true; |
| 25 |
} |
| 26 |
|
| 27 |
public static function setRequestType($requestType = null) |
| 28 |
{ |
| 29 |
self::$requestType = $requestType; |
| 30 |
} |
| 31 |
|
| 32 |
public static function clearRequestType() |
| 33 |
{ |
| 34 |
self::$requestType = null; |
| 35 |
} |
| 36 |
|
| 37 |
public static function timer() |
| 38 |
{ |
| 39 |
$tmp = self::$time; |
| 40 |
self::$time = microtime(true); |
| 41 |
|
| 42 |
return $tmp > -1 ? self::$time - $tmp : 0; |
| 43 |
} |
| 44 |
|
| 45 |
public static function clear($id) |
| 46 |
{ |
| 47 |
|
| 48 |
/** |
| 49 |
* @var ImporterManager $importer_manager |
| 50 |
*/ |
| 51 |
$importer_manager = Container::getInstance()->get('importer_manager'); |
| 52 |
if (false === $importer_manager->is_debug()) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$log_file = self::getLogFile($id); |
| 57 |
file_put_contents($log_file, ''); |
| 58 |
} |
| 59 |
public static function write($message, $id = null, $type = 'DEBUG') |
| 60 |
{ |
| 61 |
if (self::$disabled) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if (is_null($id) && !is_null(self::$id) && intval(self::$id) > 0) { |
| 66 |
$id = self::$id; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @var ImporterManager $importer_manager |
| 71 |
*/ |
| 72 |
$importer_manager = Container::getInstance()->get('importer_manager'); |
| 73 |
if (false === $importer_manager->is_debug()) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$log_file = self::getLogFile($id); |
| 78 |
|
| 79 |
$now = DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')); |
| 80 |
$log = $now->format('Y-m-d H:i:s.u') . ' ' . $type; |
| 81 |
if (!is_null(self::$requestType)) { |
| 82 |
$log .= ' ' . self::$requestType; |
| 83 |
} |
| 84 |
|
| 85 |
$log .= ' - ' . $message; |
| 86 |
|
| 87 |
$log .= ' -memory=' . self::formatBytes(memory_get_usage(), 2); |
| 88 |
|
| 89 |
file_put_contents($log_file, $log . "\n", FILE_APPEND); |
| 90 |
} |
| 91 |
|
| 92 |
public static function debug($message, $id = null) |
| 93 |
{ |
| 94 |
self::write($message, $id, 'DEBUG'); |
| 95 |
} |
| 96 |
|
| 97 |
public static function info($message, $id = null) |
| 98 |
{ |
| 99 |
self::write($message, $id, 'INFO'); |
| 100 |
} |
| 101 |
|
| 102 |
public static function error($message, $id = null) |
| 103 |
{ |
| 104 |
self::write($message, $id, 'ERROR'); |
| 105 |
} |
| 106 |
|
| 107 |
public static function warn($message, $id = null) |
| 108 |
{ |
| 109 |
self::write($message, $id, 'WARN'); |
| 110 |
} |
| 111 |
|
| 112 |
public static function getLogFile($id = null, $url = false) |
| 113 |
{ |
| 114 |
/** |
| 115 |
* @var Filesystem $filesystem |
| 116 |
*/ |
| 117 |
$filesystem = Container::getInstance()->get('filesystem'); |
| 118 |
|
| 119 |
if (is_null($id)) { |
| 120 |
return $filesystem->get_temp_directory($url) . DIRECTORY_SEPARATOR . 'debug.log'; |
| 121 |
} |
| 122 |
|
| 123 |
return $filesystem->get_temp_directory($url) . DIRECTORY_SEPARATOR . 'debug-' . $id . '.log'; |
| 124 |
} |
| 125 |
|
| 126 |
static function formatBytes($bytes, $precision = 2) |
| 127 |
{ |
| 128 |
$units = array('B', 'KB', 'MB', 'GB', 'TB'); |
| 129 |
|
| 130 |
$bytes = max($bytes, 0); |
| 131 |
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
| 132 |
$pow = min($pow, count($units) - 1); |
| 133 |
|
| 134 |
// Uncomment one of the following alternatives |
| 135 |
$bytes /= pow(1024, $pow); |
| 136 |
// $bytes /= (1 << (10 * $pow)); |
| 137 |
|
| 138 |
return round($bytes, $precision) . ' ' . $units[$pow]; |
| 139 |
} |
| 140 |
} |
| 141 |
|