@@ -48,165 +48,165 @@ |
||
48 | 48 | */ |
49 | 49 | |
50 | 50 | class File implements IWriter, IFileBased { |
51 | - /** @var string */ |
|
52 | - protected $logFile; |
|
53 | - /** @var int */ |
|
54 | - protected $logFileMode; |
|
55 | - /** @var SystemConfig */ |
|
56 | - private $config; |
|
51 | + /** @var string */ |
|
52 | + protected $logFile; |
|
53 | + /** @var int */ |
|
54 | + protected $logFileMode; |
|
55 | + /** @var SystemConfig */ |
|
56 | + private $config; |
|
57 | 57 | |
58 | - public function __construct(string $path, string $fallbackPath = '', SystemConfig $config) { |
|
59 | - $this->logFile = $path; |
|
60 | - if (!file_exists($this->logFile)) { |
|
61 | - if( |
|
62 | - ( |
|
63 | - !is_writable(dirname($this->logFile)) |
|
64 | - || !touch($this->logFile) |
|
65 | - ) |
|
66 | - && $fallbackPath !== '' |
|
67 | - ) { |
|
68 | - $this->logFile = $fallbackPath; |
|
69 | - } |
|
70 | - } |
|
71 | - $this->config = $config; |
|
72 | - $this->logFileMode = $config->getValue('logfilemode', 0640); |
|
73 | - } |
|
58 | + public function __construct(string $path, string $fallbackPath = '', SystemConfig $config) { |
|
59 | + $this->logFile = $path; |
|
60 | + if (!file_exists($this->logFile)) { |
|
61 | + if( |
|
62 | + ( |
|
63 | + !is_writable(dirname($this->logFile)) |
|
64 | + || !touch($this->logFile) |
|
65 | + ) |
|
66 | + && $fallbackPath !== '' |
|
67 | + ) { |
|
68 | + $this->logFile = $fallbackPath; |
|
69 | + } |
|
70 | + } |
|
71 | + $this->config = $config; |
|
72 | + $this->logFileMode = $config->getValue('logfilemode', 0640); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * write a message in the log |
|
77 | - * @param string $app |
|
78 | - * @param string|array $message |
|
79 | - * @param int $level |
|
80 | - */ |
|
81 | - public function write(string $app, $message, int $level) { |
|
82 | - // default to ISO8601 |
|
83 | - $format = $this->config->getValue('logdateformat', \DateTime::ATOM); |
|
84 | - $logTimeZone = $this->config->getValue('logtimezone', 'UTC'); |
|
85 | - try { |
|
86 | - $timezone = new \DateTimeZone($logTimeZone); |
|
87 | - } catch (\Exception $e) { |
|
88 | - $timezone = new \DateTimeZone('UTC'); |
|
89 | - } |
|
90 | - $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", "")); |
|
91 | - if ($time === false) { |
|
92 | - $time = new \DateTime(null, $timezone); |
|
93 | - } else { |
|
94 | - // apply timezone if $time is created from UNIX timestamp |
|
95 | - $time->setTimezone($timezone); |
|
96 | - } |
|
97 | - $request = \OC::$server->getRequest(); |
|
98 | - $reqId = $request->getId(); |
|
99 | - $remoteAddr = $request->getRemoteAddress(); |
|
100 | - // remove username/passwords from URLs before writing the to the log file |
|
101 | - $time = $time->format($format); |
|
102 | - $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; |
|
103 | - $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; |
|
104 | - if($this->config->getValue('installed', false)) { |
|
105 | - $user = \OC_User::getUser() ? \OC_User::getUser() : '--'; |
|
106 | - } else { |
|
107 | - $user = '--'; |
|
108 | - } |
|
109 | - $userAgent = $request->getHeader('User-Agent'); |
|
110 | - if ($userAgent === '') { |
|
111 | - $userAgent = '--'; |
|
112 | - } |
|
113 | - $version = $this->config->getValue('version', ''); |
|
114 | - $entry = compact( |
|
115 | - 'reqId', |
|
116 | - 'level', |
|
117 | - 'time', |
|
118 | - 'remoteAddr', |
|
119 | - 'user', |
|
120 | - 'app', |
|
121 | - 'method', |
|
122 | - 'url', |
|
123 | - 'message', |
|
124 | - 'userAgent', |
|
125 | - 'version' |
|
126 | - ); |
|
127 | - // PHP's json_encode only accept proper UTF-8 strings, loop over all |
|
128 | - // elements to ensure that they are properly UTF-8 compliant or convert |
|
129 | - // them manually. |
|
130 | - foreach($entry as $key => $value) { |
|
131 | - if(is_string($value)) { |
|
132 | - $testEncode = json_encode($value); |
|
133 | - if($testEncode === false) { |
|
134 | - $entry[$key] = utf8_encode($value); |
|
135 | - } |
|
136 | - } |
|
137 | - } |
|
138 | - $entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
139 | - $handle = @fopen($this->logFile, 'a'); |
|
140 | - if ($this->logFileMode > 0 && (fileperms($this->logFile) & 0777) != $this->logFileMode) { |
|
141 | - @chmod($this->logFile, $this->logFileMode); |
|
142 | - } |
|
143 | - if ($handle) { |
|
144 | - fwrite($handle, $entry."\n"); |
|
145 | - fclose($handle); |
|
146 | - } else { |
|
147 | - // Fall back to error_log |
|
148 | - error_log($entry); |
|
149 | - } |
|
150 | - if (php_sapi_name() === 'cli-server') { |
|
151 | - if (!\is_string($message)) { |
|
152 | - $message = json_encode($message); |
|
153 | - } |
|
154 | - error_log($message, 4); |
|
155 | - } |
|
156 | - } |
|
75 | + /** |
|
76 | + * write a message in the log |
|
77 | + * @param string $app |
|
78 | + * @param string|array $message |
|
79 | + * @param int $level |
|
80 | + */ |
|
81 | + public function write(string $app, $message, int $level) { |
|
82 | + // default to ISO8601 |
|
83 | + $format = $this->config->getValue('logdateformat', \DateTime::ATOM); |
|
84 | + $logTimeZone = $this->config->getValue('logtimezone', 'UTC'); |
|
85 | + try { |
|
86 | + $timezone = new \DateTimeZone($logTimeZone); |
|
87 | + } catch (\Exception $e) { |
|
88 | + $timezone = new \DateTimeZone('UTC'); |
|
89 | + } |
|
90 | + $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", "")); |
|
91 | + if ($time === false) { |
|
92 | + $time = new \DateTime(null, $timezone); |
|
93 | + } else { |
|
94 | + // apply timezone if $time is created from UNIX timestamp |
|
95 | + $time->setTimezone($timezone); |
|
96 | + } |
|
97 | + $request = \OC::$server->getRequest(); |
|
98 | + $reqId = $request->getId(); |
|
99 | + $remoteAddr = $request->getRemoteAddress(); |
|
100 | + // remove username/passwords from URLs before writing the to the log file |
|
101 | + $time = $time->format($format); |
|
102 | + $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; |
|
103 | + $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; |
|
104 | + if($this->config->getValue('installed', false)) { |
|
105 | + $user = \OC_User::getUser() ? \OC_User::getUser() : '--'; |
|
106 | + } else { |
|
107 | + $user = '--'; |
|
108 | + } |
|
109 | + $userAgent = $request->getHeader('User-Agent'); |
|
110 | + if ($userAgent === '') { |
|
111 | + $userAgent = '--'; |
|
112 | + } |
|
113 | + $version = $this->config->getValue('version', ''); |
|
114 | + $entry = compact( |
|
115 | + 'reqId', |
|
116 | + 'level', |
|
117 | + 'time', |
|
118 | + 'remoteAddr', |
|
119 | + 'user', |
|
120 | + 'app', |
|
121 | + 'method', |
|
122 | + 'url', |
|
123 | + 'message', |
|
124 | + 'userAgent', |
|
125 | + 'version' |
|
126 | + ); |
|
127 | + // PHP's json_encode only accept proper UTF-8 strings, loop over all |
|
128 | + // elements to ensure that they are properly UTF-8 compliant or convert |
|
129 | + // them manually. |
|
130 | + foreach($entry as $key => $value) { |
|
131 | + if(is_string($value)) { |
|
132 | + $testEncode = json_encode($value); |
|
133 | + if($testEncode === false) { |
|
134 | + $entry[$key] = utf8_encode($value); |
|
135 | + } |
|
136 | + } |
|
137 | + } |
|
138 | + $entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
139 | + $handle = @fopen($this->logFile, 'a'); |
|
140 | + if ($this->logFileMode > 0 && (fileperms($this->logFile) & 0777) != $this->logFileMode) { |
|
141 | + @chmod($this->logFile, $this->logFileMode); |
|
142 | + } |
|
143 | + if ($handle) { |
|
144 | + fwrite($handle, $entry."\n"); |
|
145 | + fclose($handle); |
|
146 | + } else { |
|
147 | + // Fall back to error_log |
|
148 | + error_log($entry); |
|
149 | + } |
|
150 | + if (php_sapi_name() === 'cli-server') { |
|
151 | + if (!\is_string($message)) { |
|
152 | + $message = json_encode($message); |
|
153 | + } |
|
154 | + error_log($message, 4); |
|
155 | + } |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * get entries from the log in reverse chronological order |
|
160 | - * @param int $limit |
|
161 | - * @param int $offset |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - public function getEntries(int $limit=50, int $offset=0):array { |
|
165 | - $minLevel = $this->config->getValue("loglevel", ILogger::WARN); |
|
166 | - $entries = array(); |
|
167 | - $handle = @fopen($this->logFile, 'rb'); |
|
168 | - if ($handle) { |
|
169 | - fseek($handle, 0, SEEK_END); |
|
170 | - $pos = ftell($handle); |
|
171 | - $line = ''; |
|
172 | - $entriesCount = 0; |
|
173 | - $lines = 0; |
|
174 | - // Loop through each character of the file looking for new lines |
|
175 | - while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) { |
|
176 | - fseek($handle, $pos); |
|
177 | - $ch = fgetc($handle); |
|
178 | - if ($ch == "\n" || $pos == 0) { |
|
179 | - if ($line != '') { |
|
180 | - // Add the first character if at the start of the file, |
|
181 | - // because it doesn't hit the else in the loop |
|
182 | - if ($pos == 0) { |
|
183 | - $line = $ch.$line; |
|
184 | - } |
|
185 | - $entry = json_decode($line); |
|
186 | - // Add the line as an entry if it is passed the offset and is equal or above the log level |
|
187 | - if ($entry->level >= $minLevel) { |
|
188 | - $lines++; |
|
189 | - if ($lines > $offset) { |
|
190 | - $entries[] = $entry; |
|
191 | - $entriesCount++; |
|
192 | - } |
|
193 | - } |
|
194 | - $line = ''; |
|
195 | - } |
|
196 | - } else { |
|
197 | - $line = $ch.$line; |
|
198 | - } |
|
199 | - $pos--; |
|
200 | - } |
|
201 | - fclose($handle); |
|
202 | - } |
|
203 | - return $entries; |
|
204 | - } |
|
158 | + /** |
|
159 | + * get entries from the log in reverse chronological order |
|
160 | + * @param int $limit |
|
161 | + * @param int $offset |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + public function getEntries(int $limit=50, int $offset=0):array { |
|
165 | + $minLevel = $this->config->getValue("loglevel", ILogger::WARN); |
|
166 | + $entries = array(); |
|
167 | + $handle = @fopen($this->logFile, 'rb'); |
|
168 | + if ($handle) { |
|
169 | + fseek($handle, 0, SEEK_END); |
|
170 | + $pos = ftell($handle); |
|
171 | + $line = ''; |
|
172 | + $entriesCount = 0; |
|
173 | + $lines = 0; |
|
174 | + // Loop through each character of the file looking for new lines |
|
175 | + while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) { |
|
176 | + fseek($handle, $pos); |
|
177 | + $ch = fgetc($handle); |
|
178 | + if ($ch == "\n" || $pos == 0) { |
|
179 | + if ($line != '') { |
|
180 | + // Add the first character if at the start of the file, |
|
181 | + // because it doesn't hit the else in the loop |
|
182 | + if ($pos == 0) { |
|
183 | + $line = $ch.$line; |
|
184 | + } |
|
185 | + $entry = json_decode($line); |
|
186 | + // Add the line as an entry if it is passed the offset and is equal or above the log level |
|
187 | + if ($entry->level >= $minLevel) { |
|
188 | + $lines++; |
|
189 | + if ($lines > $offset) { |
|
190 | + $entries[] = $entry; |
|
191 | + $entriesCount++; |
|
192 | + } |
|
193 | + } |
|
194 | + $line = ''; |
|
195 | + } |
|
196 | + } else { |
|
197 | + $line = $ch.$line; |
|
198 | + } |
|
199 | + $pos--; |
|
200 | + } |
|
201 | + fclose($handle); |
|
202 | + } |
|
203 | + return $entries; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * @return string |
|
208 | - */ |
|
209 | - public function getLogFilePath():string { |
|
210 | - return $this->logFile; |
|
211 | - } |
|
206 | + /** |
|
207 | + * @return string |
|
208 | + */ |
|
209 | + public function getLogFilePath():string { |
|
210 | + return $this->logFile; |
|
211 | + } |
|
212 | 212 | } |