Blame view

sources/lib/private/log/errorhandler.php 2.01 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  <?php
  /**
   * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OC\Log;
  
  use OC\Log as LoggerInterface;
  
  class ErrorHandler {
  	/** @var LoggerInterface */
  	private static $logger;
a293d369c   Kload   Update sources to...
16
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
17
  	 * remove password in URLs
a293d369c   Kload   Update sources to...
18
19
20
21
22
23
24
25
  	 * @param string $msg
  	 * @return string
  	 */
  	protected static function removePassword($msg) {
  		return preg_replace('/\/\/(.*):(.*)@/', '//xxx:xxx@', $msg);
  	}
  
  	public static function register($debug=false) {
31b7f2792   Kload   Upgrade to ownclo...
26
  		$handler = new ErrorHandler();
a293d369c   Kload   Update sources to...
27
28
29
30
31
  		if ($debug) {
  			set_error_handler(array($handler, 'onAll'), E_ALL);
  		} else {
  			set_error_handler(array($handler, 'onError'));
  		}
31b7f2792   Kload   Upgrade to ownclo...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  		register_shutdown_function(array($handler, 'onShutdown'));
  		set_exception_handler(array($handler, 'onException'));
  	}
  
  	public static function setLogger(LoggerInterface $logger) {
  		self::$logger = $logger;
  	}
  
  	//Fatal errors handler
  	public static function onShutdown() {
  		$error = error_get_last();
  		if($error && self::$logger) {
  			//ob_end_clean();
  			$msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line'];
a293d369c   Kload   Update sources to...
46
  			self::$logger->critical(self::removePassword($msg), array('app' => 'PHP'));
31b7f2792   Kload   Upgrade to ownclo...
47
48
49
50
51
52
  		}
  	}
  
  	// Uncaught exception handler
  	public static function onException($exception) {
  		$msg = $exception->getMessage() . ' at ' . $exception->getFile() . '#' . $exception->getLine();
a293d369c   Kload   Update sources to...
53
  		self::$logger->critical(self::removePassword($msg), array('app' => 'PHP'));
31b7f2792   Kload   Upgrade to ownclo...
54
55
56
57
58
59
60
61
  	}
  
  	//Recoverable errors handler
  	public static function onError($number, $message, $file, $line) {
  		if (error_reporting() === 0) {
  			return;
  		}
  		$msg = $message . ' at ' . $file . '#' . $line;
a293d369c   Kload   Update sources to...
62
63
64
65
66
67
68
69
  		self::$logger->error(self::removePassword($msg), array('app' => 'PHP'));
  
  	}
  
  	//Recoverable handler which catch all errors, warnings and notices
  	public static function onAll($number, $message, $file, $line) {
  		$msg = $message . ' at ' . $file . '#' . $line;
  		self::$logger->debug(self::removePassword($msg), array('app' => 'PHP'));
31b7f2792   Kload   Upgrade to ownclo...
70
71
  
  	}
a293d369c   Kload   Update sources to...
72

31b7f2792   Kload   Upgrade to ownclo...
73
  }