PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.5
Import WP – CSV & XML Import Export for WordPress v2.7.5
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Util / Logger.php

Logger.php in Import WP – CSV & XML Import Export for WordPress 2.7.5, at class/Common/Util/Logger.php

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