[662] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/> |
---|
| 5 | * Copyright © 2019 Strangecode, LLC |
---|
| 6 | * |
---|
| 7 | * This program is free software: you can redistribute it and/or modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 10 | * (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | /* |
---|
| 22 | * Logger |
---|
| 23 | * |
---|
| 24 | * A Codebase implementation of a Logger Interface as per: |
---|
| 25 | * https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md |
---|
| 26 | * |
---|
| 27 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 28 | * @version 1.0 |
---|
| 29 | * @since 30 Jan 2019 14:30:26 |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | class Logger implements \Psr\Log\LoggerInterface |
---|
| 33 | { |
---|
| 34 | /** |
---|
| 35 | * System is unusable. |
---|
| 36 | * |
---|
| 37 | * @param string $message |
---|
| 38 | * @param array $context |
---|
| 39 | * |
---|
| 40 | * @return void |
---|
| 41 | */ |
---|
| 42 | public function emergency($message, array $context = array()) |
---|
| 43 | { |
---|
| 44 | $app =& App::getInstance(); |
---|
| 45 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_EMERG, __FILE__, __LINE__); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Action must be taken immediately. |
---|
| 50 | * |
---|
| 51 | * Example: Entire website down, database unavailable, etc. This should |
---|
| 52 | * trigger the SMS alerts and wake you up. |
---|
| 53 | * |
---|
| 54 | * @param string $message |
---|
| 55 | * @param array $context |
---|
| 56 | * |
---|
| 57 | * @return void |
---|
| 58 | */ |
---|
| 59 | public function alert($message, array $context = array()) |
---|
| 60 | { |
---|
| 61 | $app =& App::getInstance(); |
---|
| 62 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_ALERT, __FILE__, __LINE__); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Critical conditions. |
---|
| 68 | * |
---|
| 69 | * Example: Application component unavailable, unexpected exception. |
---|
| 70 | * |
---|
| 71 | * @param string $message |
---|
| 72 | * @param array $context |
---|
| 73 | * |
---|
| 74 | * @return void |
---|
| 75 | */ |
---|
| 76 | public function critical($message, array $context = array()) |
---|
| 77 | { |
---|
| 78 | $app =& App::getInstance(); |
---|
| 79 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_CRIT, __FILE__, __LINE__); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Runtime errors that do not require immediate action but should typically |
---|
| 85 | * be logged and monitored. |
---|
| 86 | * |
---|
| 87 | * @param string $message |
---|
| 88 | * @param array $context |
---|
| 89 | * |
---|
| 90 | * @return void |
---|
| 91 | */ |
---|
| 92 | public function error($message, array $context = array()) |
---|
| 93 | { |
---|
| 94 | $app =& App::getInstance(); |
---|
| 95 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_ERR, __FILE__, __LINE__); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * Exceptional occurrences that are not errors. |
---|
| 100 | * |
---|
| 101 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
---|
| 102 | * that are not necessarily wrong. |
---|
| 103 | * |
---|
| 104 | * @param string $message |
---|
| 105 | * @param array $context |
---|
| 106 | * |
---|
| 107 | * @return void |
---|
| 108 | */ |
---|
| 109 | public function warning($message, array $context = array()) |
---|
| 110 | { |
---|
| 111 | $app =& App::getInstance(); |
---|
| 112 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_WARNING, __FILE__, __LINE__); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * Normal but significant events. |
---|
| 117 | * |
---|
| 118 | * @param string $message |
---|
| 119 | * @param array $context |
---|
| 120 | * |
---|
| 121 | * @return void |
---|
| 122 | */ |
---|
| 123 | public function notice($message, array $context = array()) |
---|
| 124 | { |
---|
| 125 | $app =& App::getInstance(); |
---|
| 126 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_NOTICE, __FILE__, __LINE__); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * Interesting events. |
---|
| 131 | * |
---|
| 132 | * Example: User logs in, SQL logs. |
---|
| 133 | * |
---|
| 134 | * @param string $message |
---|
| 135 | * @param array $context |
---|
| 136 | * |
---|
| 137 | * @return void |
---|
| 138 | */ |
---|
| 139 | public function info($message, array $context = array()) |
---|
| 140 | { |
---|
| 141 | $app =& App::getInstance(); |
---|
| 142 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_INFO, __FILE__, __LINE__); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Detailed debug information. |
---|
| 147 | * |
---|
| 148 | * @param string $message |
---|
| 149 | * @param array $context |
---|
| 150 | * |
---|
| 151 | * @return void |
---|
| 152 | */ |
---|
| 153 | public function debug($message, array $context = array()) |
---|
| 154 | { |
---|
| 155 | $app =& App::getInstance(); |
---|
| 156 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Logs with an arbitrary level. |
---|
| 161 | * |
---|
| 162 | * @param mixed $level |
---|
| 163 | * @param string $message |
---|
| 164 | * @param array $context |
---|
| 165 | * |
---|
| 166 | * @return void |
---|
| 167 | */ |
---|
| 168 | public function log($level, $message, array $context = array()) |
---|
| 169 | { |
---|
| 170 | $app =& App::getInstance(); |
---|
| 171 | switch ($level) { |
---|
| 172 | case LOG_EMERG: |
---|
| 173 | case LOG_ALERT: |
---|
| 174 | case LOG_CRIT: |
---|
| 175 | case LOG_ERR: |
---|
| 176 | case LOG_WARNING: |
---|
| 177 | case LOG_NOTICE: |
---|
| 178 | case LOG_INFO: |
---|
| 179 | case LOG_DEBUG: |
---|
| 180 | $app->logMsg(sprintf('%s (%s)', (string)$message, getDump($context)), LOG_CRIT, __FILE__, __LINE__); |
---|
| 181 | break; |
---|
| 182 | |
---|
| 183 | default: |
---|
| 184 | throw Psr\Log\InvalidArgumentException; |
---|
| 185 | break; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * Interpolates context values into the message placeholders. |
---|
| 191 | */ |
---|
| 192 | // function interpolate($message, array $context = array()) |
---|
| 193 | // { |
---|
| 194 | // // build a replacement array with braces around the context keys |
---|
| 195 | // $replace = array(); |
---|
| 196 | // foreach ($context as $key => $val) { |
---|
| 197 | // // check that the value can be casted to string |
---|
| 198 | // if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
---|
| 199 | // $replace['{' . $key . '}'] = $val; |
---|
| 200 | // } |
---|
| 201 | // } |
---|
| 202 | // |
---|
| 203 | // // interpolate replacement values into the message and return |
---|
| 204 | // return strtr($message, $replace); |
---|
| 205 | // } |
---|
| 206 | } |
---|