/usr/share/php/Analog/Analog.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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | <?php
namespace Analog;
/**
* 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.
*/
/**
* A short and simple logging class for based on the idea of using closures for
* configurability and extensibility. Functions as a static class, but you can
* completely control the formatting and writing of log messages through closures.
*
* By default, this class will write to a file named /tmp/log.txt using a format
* "machine - date - level - message\n".
*
* I wrote this because I wanted something simple and small like KLogger, and
* preferably not torn out of a wider framework if possible. After searching,
* I wasn't happy with the single-purpose libraries I found. With KLogger for
* example, I didn't want an object instance but rather a static class, and I
* wanted more flexibility in the back-end.
*
* I also found that the ones that had really flexible back-ends supported a lot
* that I could never personally foresee needing, and could be easier to extend
* with new back-ends that may be needed over time. Closures seem a natural fit for
* this kind of thing.
*
* What about Analog, the logfile analyzer? Well, since it hasn't been updated
* since 2004, I think it's safe to call a single-file PHP logging class the
* same thing without it being considered stepping on toes :)
*
* Usage:
*
* <?php
*
* require_once ('Analog.php');
*
* // Default logging to /tmp/analog.txt
* Analog::log ('Log this error');
*
* // Log to a MongoDB log collection
* Analog::handler (function ($info) {
* static $conn = null;
* if (! $conn) {
* $conn = new Mongo ('localhost:27017');
* }
* $conn->mydb->log->insert ($info);
* });
*
* // Log an alert
* Analog::log ('The sky is falling!', Analog::ALERT);
*
* // Log some debug info
* Analog::log ('Debugging info', Analog::DEBUG);
*
* ?>
*
* @package Analog
* @author Johnny Broadway
*/
class Analog {
/**
* List of severity levels.
*/
const URGENT = 0; // It's an emergency
const ALERT = 1; // Immediate action required
const CRITICAL = 2; // Critical conditions
const ERROR = 3; // An error occurred
const WARNING = 4; // Something unexpected happening
const NOTICE = 5; // Something worth noting
const INFO = 6; // Information, not an error
const DEBUG = 7; // Debugging messages
/**
* The default format for log messages (machine, date, level, message)
* written to a file. To change the order of items in the string,
* use `%1$s` references.
*/
public static $format = "%s - %s - %d - %s\n";
/**
* The default date/time format for log messages written to a file.
* Feeds into the `$format` property.
*/
public static $date_format = 'Y-m-d H:i:s';
/**
* Timezone for date/time values.
*/
public static $timezone = 'GMT';
/**
* Default log level.
*/
public static $default_level = 3;
/**
* The method of saving the log output. See Analog::handler()
* for details on setting this.
*/
private static $handler = null;
/**
* The name of the current machine, defaults to $_SERVER['SERVER_ADDR']
* on first call to format_message(), or 'localhost' if $_SERVER['SERVER_ADDR']
* is not set (e.g., during CLI use).
*/
public static $machine = null;
/**
* Handler getter/setter. If no handler is provided, it will set it to
* sys_get_temp_dir() . '/analog.txt' as a default. Usage:
*
* Analog::handler ('my_log.txt');
*
* Using a closure:
*
* Analog::handler (function ($msg) {
* return error_log ($msg);
* });
*/
public static function handler ($handler = false) {
if ($handler) {
self::$handler = $handler;
} elseif (! self::$handler) {
self::$handler = realpath (sys_get_temp_dir ()) . DIRECTORY_SEPARATOR . 'analog.txt';
}
return self::$handler;
}
/**
* Get the log info as an associative array.
*/
private static function get_struct ($message, $level) {
if (self::$machine === null) {
self::$machine = (isset ($_SERVER['SERVER_ADDR'])) ? $_SERVER['SERVER_ADDR'] : 'localhost';
}
$dt = new \DateTime ('now', new \DateTimeZone (self::$timezone));
return array (
'machine' => self::$machine,
'date' => $dt->format (self::$date_format),
'level' => $level,
'message' => $message
);
}
/**
* Write a raw message to the log using a function or the default
* file logging.
*/
private static function write ($struct) {
$handler = self::handler ();
if (! $handler instanceof \Closure) {
$handler = \Analog\Handler\File::init ($handler);
}
return $handler ($struct);
}
/**
* This is the main function you will call to log messages.
* Defaults to severity level Analog::ERROR, which can be
* changed via the `$default_level` property.
* Usage:
*
* Analog::log ('Debug info', Analog::DEBUG);
*/
public static function log ($message, $level = null) {
$level = ($level !== null) ? $level : self::$default_level;
return self::write (self::get_struct ($message, $level));
}
/**
* Shortcut method for Analog::log($info, Analog::URGENT)
* Usage:
*
* Analog::urgent ('Debug info');
*/
public static function urgent ($message) {
return self::write (self::get_struct ($message, self::URGENT));
}
/**
* Shortcut method for Analog::log($info, Analog::ALERT)
* Usage:
*
* Analog::alert ('Debug info');
*/
public static function alert ($message) {
return self::write (self::get_struct ($message, self::ALERT));
}
/**
* Shortcut method for Analog::log($info, Analog::ERROR)
* Usage:
*
* Analog::error ('Debug info');
*/
public static function error ($message) {
return self::write (self::get_struct ($message, self::ERROR));
}
/**
* Shortcut method for Analog::log($info, Analog::WARNING)
* Usage:
*
* Analog::warning ('Debug info');
*/
public static function warning ($message) {
return self::write (self::get_struct ($message, self::WARNING));
}
/**
* Shortcut method for Analog::log($info, Analog::NOTICE)
* Usage:
*
* Analog::notice ('Debug info');
*/
public static function notice ($message) {
return self::write (self::get_struct ($message, self::NOTICE));
}
/**
* Shortcut method for Analog::log($info, Analog::INFO)
* Usage:
*
* Analog::info ('Debug info');
*/
public static function info ($message) {
return self::write (self::get_struct ($message, self::INFO));
}
/**
* Shortcut method for Analog::log($info, Analog::DEBUG)
* Usage:
*
* Analog::debug ('Debug info');
*/
public static function debug ($message) {
return self::write (self::get_struct ($message, self::DEBUG));
}
}
|