<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

/**
 * Logger class. Uses the Zend logger.
 * EMERG   = 0;  // Emergency: system is unusable
 * ALERT   = 1;  // Alert: action must be taken immediately
 * CRIT    = 2;  // Critical: critical conditions
 * ERR     = 3;  // Error: error conditions
 * WARN    = 4;  // Warning: warning conditions
 * NOTICE  = 5;  // Notice: normal but significant condition
 * INFO    = 6;  // Informational: informational messages
 * DEBUG   = 7;  // Debug: debug messages
 * 
 */
 class Logger
{
    static protected $logger = null;
    
    static public function init(){
		self::$logger = new Zend\Log\Logger();
		$baseFilename = LOGGER_FILENAME;
        switch(strtolower(LOGGER_FREQUENCY)){
            case 'daily':
                $logFilename = $baseFilename.'_'.date('Y_m_d') . ".log";
                break;
             case 'weekly':
                $logFilename = $baseFilename.'_'.date('Y_W') . ".log";
                break;
             case 'monthly':
                $logFilename = $baseFilename.'_'.date('Y_m') . ".log";
                break;
             case 'yearly':
                $logFilename = $baseFilename.'_'.date('Y') . ".log";
                break;
             default:
                $logFilename = $baseFilename;
         }
		self::$logger->addWriter(new Zend\Log\Writer\Stream($logFilename));
	}
	
	static public function setUserName(){
		global $session;
		$usr = (isset($session->userDisplayName) ? $session->userDisplayName : "");
		return $usr;
	}
	
	static public function info($msg){
		if (LOGGER_ENABLED){
			if (self::$logger==null) 
				self::init();
			if(LOGGER_MAX_LEVEL>=Zend\Log\Logger::INFO){
				self::$logger->info("[" . self::setUserName() . "] - " . $msg);
			}
		}
	}
	
	static public function err($msg){
		if (LOGGER_ENABLED){
			if (self::$logger==null) 
				self::init();
			if(LOGGER_MAX_LEVEL>=Zend\Log\Logger::ERR){
				self::$logger->err("[" . self::setUserName() . "] - " . $msg);
			}
		}
	}
	
	static public function notice($msg){
		if (LOGGER_ENABLED){
			if (self::$logger==null) 
				self::init();
			if(LOGGER_MAX_LEVEL>=Zend\Log\Logger::NOTICE){
				self::$logger->notice("[" . self::setUserName() . "] - " . $msg);
			}
		}
	}
	
	static public function warn($msg){
		if (LOGGER_ENABLED){
			if (self::$logger==null) 
				self::init();
			if(LOGGER_MAX_LEVEL>=Zend\Log\Logger::WARN){
				self::$logger->warn("[" . self::setUserName() . "] - " . $msg);
			}
		}
	}
	
	static public function debug($msg){
		if (LOGGER_ENABLED){
			if (self::$logger==null) 
					self::init();
				if(LOGGER_MAX_LEVEL>=Zend\Log\Logger::DEBUG){
					self::$logger->debug("[" . self::setUserName() . "] - " . $msg);
				}
		}
	}	
}

?>