/usr/share/php/Icinga/Application/Logger.php is in php-icinga 2.1.0-1ubuntu1.
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Application;
use Exception;
use Icinga\Data\ConfigObject;
use Icinga\Application\Logger\Writer\FileWriter;
use Icinga\Application\Logger\Writer\SyslogWriter;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\IcingaException;
/**
* Logger
*/
class Logger
{
/**
* Debug message
*/
const DEBUG = 1;
/**
* Informational message
*/
const INFO = 2;
/**
* Warning message
*/
const WARNING = 4;
/**
* Error message
*/
const ERROR = 8;
/**
* Log levels
*
* @var array
*/
public static $levels = array(
Logger::DEBUG => 'DEBUG',
Logger::INFO => 'INFO',
Logger::WARNING => 'WARNING',
Logger::ERROR => 'ERROR'
);
/**
* This logger's instance
*
* @var static
*/
protected static $instance;
/**
* Log writer
*
* @var \Icinga\Application\Logger\LogWriter
*/
protected $writer;
/**
* Maximum level to emit
*
* @var int
*/
protected $level;
/**
* Error messages to be displayed prior to any other log message
*
* @var array
*/
protected $configErrors = array();
/**
* Create a new logger object
*
* @param ConfigObject $config
*
* @throws ConfigurationError If the logging configuration directive 'log' is missing or if the logging level is
* not defined
*/
public function __construct(ConfigObject $config)
{
if ($config->log === null) {
throw new ConfigurationError('Required logging configuration directive \'log\' missing');
}
$this->setLevel($config->get('level', static::ERROR));
if (strtolower($config->get('log', 'syslog')) !== 'none') {
$this->writer = $this->createWriter($config);
}
}
/**
* Set the logging level to use
*
* @param mixed $level
*
* @return $this
*
* @throws ConfigurationError In case the given level is invalid
*/
public function setLevel($level)
{
if (is_numeric($level)) {
$level = (int) $level;
if (! isset(static::$levels[$level])) {
throw new ConfigurationError(
'Can\'t set logging level %d. Logging level is invalid. Use one of %s or one of the'
. ' Logger\'s constants.',
$level,
implode(', ', array_keys(static::$levels))
);
}
$this->level = $level;
} else {
$level = strtoupper($level);
$levels = array_flip(static::$levels);
if (! isset($levels[$level])) {
throw new ConfigurationError(
'Can\'t set logging level "%s". Logging level is invalid. Use one of %s.',
$level,
implode(', ', array_keys($levels))
);
}
$this->level = $levels[$level];
}
return $this;
}
/**
* Return the logging level being used
*
* @return int
*/
public function getLevel()
{
return $this->level;
}
/**
* Register the given message as config error
*
* Config errors are logged every time a log message is being logged.
*
* @param mixed $arg,... A string, exception or format-string + substitutions
*
* @return $this
*/
public function registerConfigError()
{
if (func_num_args() > 0) {
$this->configErrors[] = static::formatMessage(func_get_args());
}
return $this;
}
/**
* Create a new logger object
*
* @param ConfigObject $config
*
* @return static
*/
public static function create(ConfigObject $config)
{
static::$instance = new static($config);
return static::$instance;
}
/**
* Create a log writer
*
* @param ConfigObject $config The configuration to initialize the writer with
*
* @return \Icinga\Application\Logger\LogWriter The requested log writer
* @throws ConfigurationError If the requested writer cannot be found
*/
protected function createWriter(ConfigObject $config)
{
$class = 'Icinga\\Application\\Logger\\Writer\\' . ucfirst(strtolower($config->log)) . 'Writer';
if (! class_exists($class)) {
throw new ConfigurationError(
'Cannot find log writer of type "%s"',
$config->log
);
}
return new $class($config);
}
/**
* Log a message
*
* @param int $level The logging level
* @param string $message The log message
*/
public function log($level, $message)
{
if ($this->writer !== null && $this->level <= $level) {
foreach ($this->configErrors as $error_message) {
$this->writer->log(static::ERROR, $error_message);
}
$this->writer->log($level, $message);
}
}
/**
* Return a string representation of the passed arguments
*
* This method provides three different processing techniques:
* - If the only passed argument is a string it is returned unchanged
* - If the only passed argument is an exception it is formatted as follows:
* <name> in <file>:<line> with message: <message>[ <- <name> ...]
* - If multiple arguments are passed the first is interpreted as format-string
* that gets substituted with the remaining ones which can be of any type
*
* @param array $arguments The arguments to format
*
* @return string The formatted result
*/
protected static function formatMessage(array $arguments)
{
if (count($arguments) === 1) {
$message = $arguments[0];
if ($message instanceof Exception) {
$messages = array();
$error = $message;
do {
$messages[] = IcingaException::describe($error);
} while ($error = $error->getPrevious());
$message = implode(' <- ', $messages);
}
return $message;
}
return vsprintf(
array_shift($arguments),
array_map(
function ($a) {
return is_string($a) ? $a : ($a instanceof Exception
? IcingaException::describe($a)
: json_encode($a));
},
$arguments
)
);
}
/**
* Log a message with severity ERROR
*
* @param mixed $arg,... A string, exception or format-string + substitutions
*/
public static function error()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::ERROR, static::formatMessage(func_get_args()));
}
}
/**
* Log a message with severity WARNING
*
* @param mixed $arg,... A string, exception or format-string + substitutions
*/
public static function warning()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::WARNING, static::formatMessage(func_get_args()));
}
}
/**
* Log a message with severity INFO
*
* @param mixed $arg,... A string, exception or format-string + substitutions
*/
public static function info()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::INFO, static::formatMessage(func_get_args()));
}
}
/**
* Log a message with severity DEBUG
*
* @param mixed $arg,... A string, exception or format-string + substitutions
*/
public static function debug()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::DEBUG, static::formatMessage(func_get_args()));
}
}
/**
* Get the log writer to use
*
* @return \Icinga\Application\Logger\LogWriter
*/
public function getWriter()
{
return $this->writer;
}
/**
* Is the logger writing to Syslog?
*
* @return bool
*/
public static function writesToSyslog()
{
return static::$instance && static::$instance->getWriter() instanceof SyslogWriter;
}
/**
* Is the logger writing to a file?
*
* @return bool
*/
public static function writesToFile()
{
return static::$instance && static::$instance->getWriter() instanceof FileWriter;
}
/**
* Get this' instance
*
* @return static
*/
public static function getInstance()
{
return static::$instance;
}
}
|