5
5
6
6
namespace Magento \Setup \Model ;
7
7
8
+ use Magento \Framework \App \Filesystem \DirectoryList ;
9
+ use Magento \Framework \Filesystem ;
10
+
8
11
/**
9
12
* Web UI Logger
10
13
*
@@ -22,16 +25,17 @@ class WebLogger implements LoggerInterface
22
25
/**
23
26
* Currently open file resource
24
27
*
25
- * @var resource
28
+ * @var Filesystem
26
29
*/
27
- protected $ resource ;
30
+ protected $ filesystem ;
31
+
28
32
29
33
/**
30
- * Whether the log contains an error message
34
+ * Currently open file resource
31
35
*
32
- * @var bool
36
+ * @var \Magento\Framework\Filesystem\Directory\WriteInterface
33
37
*/
34
- protected $ hasError = false ;
38
+ protected $ directory ;
35
39
36
40
/**
37
41
* Indicator of whether inline output is started
@@ -42,31 +46,11 @@ class WebLogger implements LoggerInterface
42
46
43
47
/**
44
48
* Constructor
49
+ * @param Filesystem $filesystem
45
50
*/
46
- public function __construct ()
47
- {
48
- $ this ->logFile = sys_get_temp_dir () . DIRECTORY_SEPARATOR . $ this ->logFile ;
49
- }
50
-
51
- /**
52
- * Opens log file in the specified mode
53
- *
54
- * @param string $mode
55
- * @return void
56
- */
57
- private function open ($ mode )
58
- {
59
- $ this ->resource = fopen ($ this ->logFile , $ mode );
60
- }
61
-
62
- /**
63
- * Closes the log file
64
- *
65
- * @return void
66
- */
67
- private function close ()
51
+ public function __construct (Filesystem $ filesystem )
68
52
{
69
- fclose ( $ this ->resource );
53
+ $ this ->directory = $ filesystem -> getDirectoryWrite (DirectoryList:: LOG );
70
54
}
71
55
72
56
/**
@@ -84,7 +68,14 @@ public function logSuccess($message)
84
68
public function logError (\Exception $ e )
85
69
{
86
70
$ this ->terminateLine ();
87
- $ this ->writeToFile ('<span class="text-danger">[ERROR] ' . $ e . '<span><br/> ' );
71
+ $ stackTrace = $ e ->getTrace ();
72
+ $ this ->writeToFile ('<span class="text-danger">[ERROR] ' . $ e ->getMessage () . '<br/> ' );
73
+ foreach ($ stackTrace as $ errorLine ) {
74
+ if (isset ($ errorLine ['file ' ])) {
75
+ $ this ->writeToFile ($ errorLine ['file ' ] . ' ' . $ errorLine ['line ' ] . '<br/> ' );
76
+ }
77
+ }
78
+ $ this ->writeToFile ('<span><br/> ' );
88
79
}
89
80
90
81
/**
@@ -122,19 +113,7 @@ public function logMeta($message)
122
113
*/
123
114
private function writeToFile ($ message )
124
115
{
125
- $ this ->open ('a+ ' );
126
- fwrite ($ this ->resource , $ message );
127
- $ this ->close ();
128
- }
129
-
130
- /**
131
- * Whether there is an error in the log
132
- *
133
- * @return bool
134
- */
135
- public function hasError ()
136
- {
137
- return $ this ->hasError ;
116
+ $ this ->directory ->writeFile ($ this ->logFile , $ message , 'a+ ' );
138
117
}
139
118
140
119
/**
@@ -144,17 +123,8 @@ public function hasError()
144
123
*/
145
124
public function get ()
146
125
{
147
- $ this ->open ('r+ ' );
148
- fseek ($ this ->resource , 0 );
149
- $ messages = [];
150
- while (($ string = fgets ($ this ->resource )) !== false ) {
151
- if (strpos ($ string , '[ERROR] ' ) !== false ) {
152
- $ this ->hasError = true ;
153
- }
154
- $ messages [] = $ string ;
155
- }
156
- $ this ->close ();
157
- return $ messages ;
126
+ $ fileContents = explode ('\n ' , $ this ->directory ->readFile ($ this ->logFile ));
127
+ return $ fileContents ;
158
128
}
159
129
160
130
/**
@@ -164,8 +134,8 @@ public function get()
164
134
*/
165
135
public function clear ()
166
136
{
167
- if (file_exists ($ this ->logFile )) {
168
- unlink ($ this ->logFile );
137
+ if ($ this -> directory -> isExist ($ this ->logFile )) {
138
+ $ this -> directory -> delete ($ this ->logFile );
169
139
}
170
140
}
171
141
0 commit comments