Fix our Logger not supporting named logging levels and requiring the 'enabled' directive

This commit is contained in:
Eric Lippmann 2014-10-16 15:54:13 +02:00
parent 985154d3d8
commit 97677ee2c1
1 changed files with 104 additions and 50 deletions

View File

@ -6,61 +6,110 @@ namespace Icinga\Logger;
use Exception;
use Zend_Config;
use LogicException;
use Icinga\Exception\ConfigurationError;
use Icinga\Logger\Writer\FileWriter;
use Icinga\Logger\Writer\SyslogWriter;
/**
* Singleton logger
* 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 Logger
* @var static
*/
protected static $instance;
/**
* The log writer to use
* Log writer
*
* @var \Icinga\Logger\LogWriter
*/
protected $writer;
/**
* The configured type
*
* @string Type (syslog, file)
*/
protected $type = 'none';
/**
* The maximum severity to emit
* Maximum level to emit
*
* @var int
*/
protected $verbosity;
/**
* The supported severities
*/
public static $NONE = 0;
public static $ERROR = 1;
public static $WARNING = 2;
public static $INFO = 3;
public static $DEBUG = 4;
protected $level;
/**
* Create a new logger object
*
* @param Zend_Config $config
* @param Zend_Config $config
*
* @throws ConfigurationError If the logging configuration directive 'log' is missing or if the logging level is
* not defined
*/
public function __construct(Zend_Config $config)
{
$this->verbosity = $config->level;
if ($config->log === null) {
throw new ConfigurationError('Required logging configuration directive \'log\' missing');
}
if ($config->enable) {
if (($level = $config->level) !== null) {
if (is_numeric($level)) {
if (! isset(static::$levels[(int) $level])) {
throw new ConfigurationError(
'Can\'t set logging level %d. Logging level is not defined. Use one of %s or one of the'
. ' Logger\'s constants.',
$level,
implode(', ', array_keys(static::$levels))
);
}
$this->level = static::$levels[(int) $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 not defined. Use one of %s.',
$level,
implode(', ', array_keys($levels))
);
}
$this->level = $levels[$level];
}
} else {
$this->level = static::ERROR;
}
if (strtolower($config->get('log', 'syslog')) !== 'none') {
$this->writer = $this->createWriter($config);
}
}
@ -69,14 +118,17 @@ class Logger
* Create a new logger object
*
* @param Zend_Config $config
*
* @return static
*/
public static function create(Zend_Config $config)
{
static::$instance = new static($config);
return static::$instance;
}
/**
* Return a log writer
* Create a log writer
*
* @param Zend_Config $config The configuration to initialize the writer with
*
@ -85,34 +137,26 @@ class Logger
*/
protected function createWriter(Zend_Config $config)
{
$class = 'Icinga\\Logger\\Writer\\' . ucfirst(strtolower($config->type)) . 'Writer';
if (!class_exists($class)) {
$class = 'Icinga\\Logger\\Writer\\' . ucfirst(strtolower($config->log)) . 'Writer';
if (! class_exists($class)) {
throw new ConfigurationError(
'Cannot find log writer of type "%s"',
$config->type
$config->log
);
}
$this->type = $config->type;
return new $class($config);
}
/**
* Write a message to the log
* Log a message
*
* @param string $message The message to write
* @param int $severity The severity to use
*
* @throws LogicException In case $severity equals self::$NONE
* @param int $level The logging level
* @param string $message The log message
*/
public function log($message, $severity)
public function log($level, $message)
{
if ($severity === static::$NONE) {
throw new LogicException("`None' (0) is not a valid severity to log messages");
}
if ($this->writer !== null && $this->verbosity >= $severity) {
$this->writer->log($severity, $message);
if ($this->writer !== null && $this->level >= $level) {
$this->writer->log($level, $message);
}
}
@ -170,7 +214,7 @@ class Logger
public static function error()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::formatMessage(func_get_args()), static::$ERROR);
static::$instance->log(static::ERROR, static::formatMessage(func_get_args()));
}
}
@ -182,7 +226,7 @@ class Logger
public static function warning()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::formatMessage(func_get_args()), static::$WARNING);
static::$instance->log(static::WARNING, static::formatMessage(func_get_args()));
}
}
@ -194,7 +238,7 @@ class Logger
public static function info()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::formatMessage(func_get_args()), static::$INFO);
static::$instance->log(static::INFO, static::formatMessage(func_get_args()));
}
}
@ -206,7 +250,7 @@ class Logger
public static function debug()
{
if (static::$instance !== null && func_num_args() > 0) {
static::$instance->log(static::formatMessage(func_get_args()), static::$DEBUG);
static::$instance->log(static::DEBUG, static::formatMessage(func_get_args()));
}
}
@ -220,20 +264,30 @@ class Logger
return $this->writer;
}
/**
* Is the logger writing to Syslog?
*
* @return bool
*/
public static function writesToSyslog()
{
return static::$instance && static::$instance->type === 'syslog';
return static::$instance && static::$instance instanceof SyslogWriter;
}
/**
* Is the logger writing to a file?
*
* @return bool
*/
public static function writesToFile()
{
return static::$instance && static::$instance->type === 'file';
return static::$instance && static::$instance instanceof FileWriter;
}
/**
* Get this' instance
*
* @return Logger
* @return static
*/
public static function getInstance()
{