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