* @license LGPL http://www.gnu.org/licenses/lgpl.html * @package wb * @subpackage Log */ WBClass::load('WBString'); /** * Basic logger class and interface * * @version 0.3.1 * @package wb * @subpackage Factory * @test test/unit/log */ class WBLog extends WBStdClass { /** * log level translation table * @var string */ private static $_levels = array( 0 => 'emerg', 1 => 'alert', 2 => 'crit', 3 => 'error', 4 => 'warning', 5 => 'notice', 6 => 'info', 7 => 'debug' ); /** * message delimitor * @var string */ protected static $_delimiter = '|'; /** * message key value glue * @var string */ protected static $_glue = ':'; /** * log dateforma * @var string */ protected static $_dateFormat = 'M d H:i:s'; /** * minimum log level * @var int */ protected $_level = LOG_NOTICE; /** * logger service id * @var string */ protected $_service = null; /** * session * @var string */ protected $_session = null; /** * host name * @var string */ protected $_host = null; /** * config values * @var array */ protected $_config = array(); /** * running logger * @var array */ private static $_logger = array(); /** * configuration tool * @var WBConfig */ private static $_configLoader; /** * constructor * * @param string $service */ protected function __construct( $service, $config ) { // set log level if( isset( $config['level'] ) ) { $levels = array_flip( self::$_levels ); if( isset( $levels[strtolower( $config['level'] )] ) ) { $this->_level = $levels[strtolower( $config['level'] )]; } } $config['level'] = self::$_levels[$this->_level]; $this->_config = $config; $this->_service = $service; $this->_session = WBParam::get( 'wb/session/id', getmypid() ); $this->_host = 'localhost'; if( isset( $_SERVER['SERVER_NAME'] ) ) { $this->_host = $_SERVER['SERVER_NAME']; } } /** * start concrete logger * * Each logger is configured in config file. In case there is not config * for this logger id found, the WBParam "wb.log.default.driver" and * "wb.log.default.level" is used. In case "wb.log.default.driver" is not * set, the logger id "__default" will be fetched from configuration file. * In case this also does not exist, The really default logger will be used: * * driver: File * file: default * level: notice * * @param string $id * @return WBLog */ static public function start( $service ) { $service = str_replace( ' ', '_', $service ); if( isset( self::$_logger[$service] ) ) { return self::$_logger[$service]; } if (!self::$_configLoader) { self::$_configLoader = WBClass::create( 'WBConfig' ); self::$_configLoader->load( 'log' ); } // try to load the config $config = self::$_configLoader->get( $service ); if( !$config ) { // seek runtime default $driver = WBParam::get( 'wb/log/default/driver', false ); if( $driver ) { $config = array( 'driver' => $driver, 'level' => WBParam::get( 'wb/log/default/level', 'notice' ) ); } else { // try to load default from config file $config = self::$_configLoader->get( '__default' ); if( !$config ) { $config = array( 'driver' => 'File', 'file' => 'default', 'level' => WBParam::get( 'wb/log/default/level', 'notice' ) ); } } } // instantiate new one $clazz = 'WBLog_' . $config['driver']; WBClass::load( $clazz ); self::$_logger[$service] = new $clazz( $service, $config ); return self::$_logger[$service]; } /** * check whether level is sufficient * * @param int $level log level of message * @return bool true on success */ public function checkLevel( $level ) { if( $level > $this->_level ) { return false; } return true; } /** * Log Message * * @param mixed $msg * @return true on success */ public function debug( $msg ) { return $this->log( 7, $msg ); } /** * Log Message * * @param mixed $msg * @return true on success */ public function info( $msg ) { return $this->log( 6, $msg ); } /** * Log Message * * @param mixed $msg * @return true on success */ public function notice( $msg ) { return $this->log( 5, $msg ); } /** * Log Message * * @param mixed $msg * @return true on success */ public function warn( $msg ) { return $this->log( 4, $msg ); } /** * Log Message * * @param mixed $msg * @return true on success */ public function err( $msg ) { return $this->log( 3, $msg ); } /** * Log Message * * @param int $level * @param mixed $msg * @return true on success */ protected function log( $level, $msg ) { if (!$this->checkLevel($level)) { return true; } $levelStr = self::$_levels[$level]; return $this->write($levelStr, $this->format($msg)); } /** * format log message * * @param array|string $msg * @return string */ protected function format($msg) { if(!is_array($msg)) { return $msg; } return WBString::formatArray($msg, self::$_glue, self::$_delimiter); } } ?>