Merge branch 'MDL-78811-Master' of https://github.com/aydevworks/moodle
[moodle.git] / lib / phpxmlrpc / Helper / Logger.php
bloba934611f57b3ba5dac274ee3eeec07aaa1cea7b0
1 <?php
3 namespace PhpXmlRpc\Helper;
5 /**
6 * @todo implement an interface
7 * @todo make constructor private to force users to go through `instance()` ?
8 */
9 class Logger
11 protected static $instance = null;
13 /**
14 * This class can be used as singleton, so that later we can move to DI patterns (ish...)
16 * @return Logger
18 public static function instance()
20 if (self::$instance === null) {
21 self::$instance = new self();
24 return self::$instance;
27 // *** Implement the same interface as PSR/LOG, for the sake of interoperability ***
29 /**
30 * NB: unlike other "traditional" loggers, this one echoes to screen the debug messages instead of logging them.
32 * @param string $message
33 * @param array $context known key: 'encoding'
34 * @return void
36 public function debug($message, $context = array())
38 if (isset($context['encoding'])) {
39 $this->debugMessage($message, $context['encoding']);
40 } else {
41 $this->debugMessage($message);
45 /**
46 * Following the general principle of 'never break stdout', the default behaviour
48 * @param string $message
49 * @param $context
50 * @return void
52 public function warning($message, $context = array())
54 $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Warning: ', $message));
57 /**
58 * Triggers the writing of a message to php's error log
60 * @param string $message
61 * @param array $context
62 * @return void
64 public function error($message, $context = array())
66 $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Error: ', $message));
69 // BC interface
71 /**
72 * Echoes a debug message, taking care of escaping it when not in console mode.
73 * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
74 * of 100% accuracy, which kind of defeats the purpose of debugging
76 * @param string $message
77 * @param string $encoding deprecated
78 * @return void
80 * @internal left in purely for BC
82 public function debugMessage($message, $encoding = null)
84 // US-ASCII is a warning for PHP and a fatal for HHVM
85 if ($encoding == 'US-ASCII') {
86 $encoding = 'UTF-8';
89 if (PHP_SAPI != 'cli') {
90 $flags = ENT_COMPAT;
91 // avoid warnings on php < 5.4...
92 if (defined('ENT_HTML401')) {
93 $flags = $flags | ENT_HTML401;
95 if (defined('ENT_SUBSTITUTE')) {
96 $flags = $flags | ENT_SUBSTITUTE;
98 if ($encoding != null) {
99 print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
100 } else {
101 print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
103 } else {
104 print "\n$message\n";
107 // let the user see this now in case there's a time-out later...
108 flush();
112 * Writes a message to the error log.
114 * @param string $message
115 * @return void
117 * @internal left in purely for BC
119 public function errorLog($message)
121 error_log($message);