mb_log_level)) { $n = "class_mb_exception: please set LOG_LEVEL in mapbender.conf" . $n; } if ($this->isValidLevel($level)) { if (is_dir($this->dir)) { $logfile = $this->dir . $this->filename_prefix . date("Y_m_d") . ".log"; if ($h = fopen($logfile,"a")) { $content = date("Y.m.d, H:i:s") . "," . $n .chr(13).chr(10); if(!fwrite($h,$content)){ $this->result = false; $this->message = "Unable to write " . $logfile; return false; } fclose($h); $this->result = true; $this->message = "Successful."; return true; } else { $this->result = false; $this->message = "Unable to open or generate " . $logfile; return false; } } else { $this->result = false; $this->message = "Directory " . $this->dir . " is not valid."; return false; } } else { $this->result = false; $this->message = "Log level '" . $level . "' is not valid or logging is disabled in mapbender.conf."; return false; } } /** * Retrieves the index of the level in the array of available levels. * By this, we can find out if the message is eligable for logging. * * @param string $level the log level of the message * @param string[] $levelArray an array of available levels * @return mixed false, if the level is not available; else * the index of the level in the log level array */ protected function indexOf ($level, $levelArray) { $index = false; for ($i=0; $i < count($levelArray); $i++) { if ($levelArray[$i] == $level) { $index = $i; } } return $index; } /** * Checks if the message will be logged. Example: Log level of the message is "warning", * but the log level set in * {@link http://www.mapbender.org/index.php/Mapbender.conf#Mapbender_error_logging mapbender.conf} * is "off", then the message will not be logged. * * @param string $level the log level of the message that is being logged. * @return bool true if the message will be logged; else false. */ protected function isValidLevel ($level) { $log_level_array = explode(",", $this->log_levels); $isValid = in_array($level, $log_level_array); $isAppropriate = ($this->indexOf($level, $log_level_array) <= $this->indexOf($this->mb_log_level, $log_level_array)); return $isValid && $isAppropriate; } /** * @var string a comma-separated list of available log levels, see * {@link http://www.mapbender.org/index.php/Mapbender.conf#Mapbender_error_logging mapbender.conf}. */ protected $log_levels = LOG_LEVEL_LIST; /** * @var string the selected log level, see * {@link http://www.mapbender.org/index.php/Mapbender.conf#Mapbender_error_logging mapbender.conf}. */ protected $mb_log_level = LOG_LEVEL; /** * @var string the path to the log directory */ protected $dir = "../../log/"; /** * @var string the prefix of the logs' file name */ protected $filename_prefix = "mb_error_"; /** * @var bool true if the logging succeeded; else false. */ public $result = false; /** * @var string if the logging did not succeed, this contains an error message. */ public $message = ""; } ?>