Skip to content

Commit

Permalink
Fix our Logger not supporting named logging levels and requiring the …
Browse files Browse the repository at this point in the history
…'enabled' directive
  • Loading branch information
lippserd committed Oct 16, 2014
1 parent 985154d commit 97677ee
Showing 1 changed file with 103 additions and 49 deletions.
152 changes: 103 additions & 49 deletions library/Icinga/Logger/Logger.php
Expand Up @@ -6,61 +6,110 @@

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
{
/**
* This logger's instance
*
* @var Logger
* Debug message
*/
protected static $instance;
const DEBUG = 1;

/**
* Informational message
*/
const INFO = 2;

/**
* The log writer to use
* Warning message
*/
const WARNING = 4;

/**
* Error message
*/
const ERROR = 8;

/**
* Log levels
*
* @var \Icinga\Logger\LogWriter
* @var array
*/
protected $writer;
public static $levels = array(
Logger::DEBUG => 'DEBUG',
Logger::INFO => 'INFO',
Logger::WARNING => 'WARNING',
Logger::ERROR => 'ERROR'
);

/**
* The configured type
* This logger's instance
*
* @string Type (syslog, file)
* @var static
*/
protected $type = 'none';
protected static $instance;

/**
* The maximum severity to emit
* Log writer
*
* @var int
* @var \Icinga\Logger\LogWriter
*/
protected $verbosity;
protected $writer;

/**
* The supported severities
* Maximum level to emit
*
* @var int
*/
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 (($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 ($config->enable) {
if (strtolower($config->get('log', 'syslog')) !== 'none') {
$this->writer = $this->createWriter($config);
}
}
Expand All @@ -69,14 +118,17 @@ public function __construct(Zend_Config $config)
* 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
*
Expand All @@ -85,34 +137,26 @@ public static function create(Zend_Config $config)
*/
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
*
* @param string $message The message to write
* @param int $severity The severity to use
* Log a message
*
* @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);
}
}

Expand Down Expand Up @@ -170,7 +214,7 @@ function ($a) { return is_string($a) ? $a : json_encode($a); },
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()));
}
}

Expand All @@ -182,7 +226,7 @@ public static function error()
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()));
}
}

Expand All @@ -194,7 +238,7 @@ public static function warning()
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()));
}
}

Expand All @@ -206,7 +250,7 @@ public static function info()
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()));
}
}

Expand All @@ -220,20 +264,30 @@ public function getWriter()
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()
{
Expand Down

0 comments on commit 97677ee

Please sign in to comment.