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