Completed
Push — master ( 75657c...52eaf6 )
by Morris
16:33
created
lib/private/Log/File.php 1 patch
Indentation   +142 added lines, -142 removed lines patch added patch discarded remove patch
@@ -41,152 +41,152 @@
 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
-			'remoteAddr',
110
-			'app',
111
-			'message',
112
-			'level',
113
-			'time',
114
-			'method',
115
-			'url',
116
-			'user',
117
-			'userAgent',
118
-			'version'
119
-		);
120
-		$entry = json_encode($entry);
121
-		$handle = @fopen(self::$logFile, 'a');
122
-		if ((fileperms(self::$logFile) & 0777) != 0640) {
123
-			@chmod(self::$logFile, 0640);
124
-		}
125
-		if ($handle) {
126
-			fwrite($handle, $entry."\n");
127
-			fclose($handle);
128
-		} else {
129
-			// Fall back to error_log
130
-			error_log($entry);
131
-		}
132
-		if (php_sapi_name() === 'cli-server') {
133
-			error_log($message, 4);
134
-		}
135
-	}
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
+            'remoteAddr',
110
+            'app',
111
+            'message',
112
+            'level',
113
+            'time',
114
+            'method',
115
+            'url',
116
+            'user',
117
+            'userAgent',
118
+            'version'
119
+        );
120
+        $entry = json_encode($entry);
121
+        $handle = @fopen(self::$logFile, 'a');
122
+        if ((fileperms(self::$logFile) & 0777) != 0640) {
123
+            @chmod(self::$logFile, 0640);
124
+        }
125
+        if ($handle) {
126
+            fwrite($handle, $entry."\n");
127
+            fclose($handle);
128
+        } else {
129
+            // Fall back to error_log
130
+            error_log($entry);
131
+        }
132
+        if (php_sapi_name() === 'cli-server') {
133
+            error_log($message, 4);
134
+        }
135
+    }
136 136
 
137
-	/**
138
-	 * get entries from the log in reverse chronological order
139
-	 * @param int $limit
140
-	 * @param int $offset
141
-	 * @return array
142
-	 */
143
-	public static function getEntries($limit=50, $offset=0) {
144
-		self::init();
145
-		$minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", \OCP\Util::WARN);
146
-		$entries = array();
147
-		$handle = @fopen(self::$logFile, 'rb');
148
-		if ($handle) {
149
-			fseek($handle, 0, SEEK_END);
150
-			$pos = ftell($handle);
151
-			$line = '';
152
-			$entriesCount = 0;
153
-			$lines = 0;
154
-			// Loop through each character of the file looking for new lines
155
-			while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) {
156
-				fseek($handle, $pos);
157
-				$ch = fgetc($handle);
158
-				if ($ch == "\n" || $pos == 0) {
159
-					if ($line != '') {
160
-						// Add the first character if at the start of the file,
161
-						// because it doesn't hit the else in the loop
162
-						if ($pos == 0) {
163
-							$line = $ch.$line;
164
-						}
165
-						$entry = json_decode($line);
166
-						// Add the line as an entry if it is passed the offset and is equal or above the log level
167
-						if ($entry->level >= $minLevel) {
168
-							$lines++;
169
-							if ($lines > $offset) {
170
-								$entries[] = $entry;
171
-								$entriesCount++;
172
-							}
173
-						}
174
-						$line = '';
175
-					}
176
-				} else {
177
-					$line = $ch.$line;
178
-				}
179
-				$pos--;
180
-			}
181
-			fclose($handle);
182
-		}
183
-		return $entries;
184
-	}
137
+    /**
138
+     * get entries from the log in reverse chronological order
139
+     * @param int $limit
140
+     * @param int $offset
141
+     * @return array
142
+     */
143
+    public static function getEntries($limit=50, $offset=0) {
144
+        self::init();
145
+        $minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", \OCP\Util::WARN);
146
+        $entries = array();
147
+        $handle = @fopen(self::$logFile, 'rb');
148
+        if ($handle) {
149
+            fseek($handle, 0, SEEK_END);
150
+            $pos = ftell($handle);
151
+            $line = '';
152
+            $entriesCount = 0;
153
+            $lines = 0;
154
+            // Loop through each character of the file looking for new lines
155
+            while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) {
156
+                fseek($handle, $pos);
157
+                $ch = fgetc($handle);
158
+                if ($ch == "\n" || $pos == 0) {
159
+                    if ($line != '') {
160
+                        // Add the first character if at the start of the file,
161
+                        // because it doesn't hit the else in the loop
162
+                        if ($pos == 0) {
163
+                            $line = $ch.$line;
164
+                        }
165
+                        $entry = json_decode($line);
166
+                        // Add the line as an entry if it is passed the offset and is equal or above the log level
167
+                        if ($entry->level >= $minLevel) {
168
+                            $lines++;
169
+                            if ($lines > $offset) {
170
+                                $entries[] = $entry;
171
+                                $entriesCount++;
172
+                            }
173
+                        }
174
+                        $line = '';
175
+                    }
176
+                } else {
177
+                    $line = $ch.$line;
178
+                }
179
+                $pos--;
180
+            }
181
+            fclose($handle);
182
+        }
183
+        return $entries;
184
+    }
185 185
 
186
-	/**
187
-	 * @return string
188
-	 */
189
-	public static function getLogFilePath() {
190
-		return self::$logFile;
191
-	}
186
+    /**
187
+     * @return string
188
+     */
189
+    public static function getLogFilePath() {
190
+        return self::$logFile;
191
+    }
192 192
 }
Please login to merge, or discard this patch.