/usr/share/php/Analog/Logger.php is in php-analog 1.0.7-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | <?php
namespace Analog;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\InvalidArgumentException;
/**
* Analog - PHP 5.3+ logging class
*
* Copyright (c) 2012 Johnny Broadway
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Implements the PSR-3 standard as a wrapper to Analog. For more information,
* see:
*
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
*
* Usage:
*
* <?php
*
* require_once ('vendor/autoload.php');
*
* $log = new Analog\Logger ();
*
* $log->notice ('Things are really happening right now.');
*
* ?>
*
* @package Analog
* @author Johnny Broadway
*/
class Logger implements LoggerInterface {
/**
* Converts from PSR-3 log levels to Analog log levels.
*/
public function convert_log_level ($level, $reverse = false) {
if ($reverse) {
switch ($level) {
case Analog::URGENT:
return LogLevel::EMERGENCY;
case Analog::ALERT:
return LogLevel::ALERT;
case Analog::CRITICAL:
return LogLevel::CRITICAL;
case Analog::ERROR:
return LogLevel::ERROR;
case Analog::WARNING:
return LogLevel::WARNING;
case Analog::NOTICE:
return LogLevel::NOTICE;
case Analog::INFO:
return LogLevel::INFO;
case Analog::DEBUG:
return LogLevel::DEBUG;
}
throw new InvalidArgumentException ('Level "' . $level . '" is not defined.');
} else {
switch ($level) {
case LogLevel::EMERGENCY:
return Analog::URGENT;
case LogLevel::ALERT:
return Analog::ALERT;
case LogLevel::CRITICAL:
return Analog::CRITICAL;
case LogLevel::ERROR:
return Analog::ERROR;
case LogLevel::WARNING:
return Analog::WARNING;
case LogLevel::NOTICE:
return Analog::NOTICE;
case LogLevel::INFO:
return Analog::INFO;
case LogLevel::DEBUG:
return Analog::DEBUG;
}
throw new InvalidArgumentException ('Level "' . $level . '" is not defined.');
}
}
/**
* Interpolates context values into the message placeholders.
*/
private function interpolate ($message, array $context = array ()) {
if (is_array ($message)) {
return $message;
}
// build a replacement array with braces around the context keys
$replace = array ();
foreach ($context as $key => $val) {
if (is_object ($val) && get_class ($val) === 'DateTime') {
$val = $val->format ('Y-m-d H:i:s');
} elseif (is_object ($val)) {
$val = json_encode ($val);
} elseif (is_array ($val)) {
$val = json_encode ($val);
} elseif (is_resource ($val)) {
$val = (string) $val;
}
$replace['{' . $key . '}'] = $val;
}
// interpolate replacement values into the the message and return
return strtr ($message, $replace);
}
/**
* Sets the Analog log handler.
*/
public function handler ($handler) {
Analog::handler ($handler);
}
/**
* Sets the log message format.
*/
public function format ($format) {
Analog::$format = $format;
}
/**
* System is unusable.
*/
public function emergency ($message, array $context = array ()) {
$this->_log (Analog::URGENT, $message, $context);
}
/**
* Action must be taken immediately.
*/
public function alert ($message, array $context = array ()) {
$this->_log (Analog::ALERT, $message, $context);
}
/**
* Critical conditions.
*/
public function critical ($message, array $context = array ()) {
$this->_log (Analog::CRITICAL, $message, $context);
}
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*/
public function error ($message, array $context = array ()) {
$this->_log (Analog::ERROR, $message, $context);
}
/**
* Exceptional occurrences that are not errors.
*/
public function warning ($message, array $context = array ()) {
$this->_log (Analog::WARNING, $message, $context);
}
/**
* Normal but significant events.
*/
public function notice ($message, array $context = array ()) {
$this->_log (Analog::NOTICE, $message, $context);
}
/**
* Interesting events.
*/
public function info ($message, array $context = array ()) {
$this->_log (Analog::INFO, $message, $context);
}
/**
* Detailed debug information.
*/
public function debug ($message, array $context = array ()) {
$this->_log (Analog::DEBUG, $message, $context);
}
/**
* Logs with an arbitrary level.
*/
public function log ($level, $message, array $context = array ()) {
$this->_log (
$this->convert_log_level ($level),
$message,
$context
);
}
/**
* Perform the logging to Analog after the log level has been converted.
*/
private function _log ($level, $message, $context) {
Analog::log (
$this->interpolate ($message, $context),
$level
);
}
}
|