PluginProbe
Import WP – CSV & XML Import Export for WordPress / trunk
Import WP – CSV & XML Import Export for WordPress vtrunk
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 trunk 0.1.6 All 142 releases
jc-importer / class / Common / Util / Logger.php

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

141 lines 3.5 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 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