* @copyright 2005 by http://www.epygi.de * @package newsletter * @subpackage system */ /** * include PEAR:Log_file and param class */ wbFactory::includeClass( 'Log' ); wbFactory::includeClass( 'Log_file' ); /** * storage for used logger * * This is sort of a "fake" static variable... */ $GLOBALS['_wbLog'] = array( 'logger' => array(), 'type' => 'file', 'conf' => array( 'mode' => 0666, 'lineFormat' => '%1$s|%2$s|%3$s|%4$s', 'timeFormat' => '%Y-%m-%d %H:%M:%S', ), 'delimiter' => '|', 'logDir' => 'log', ); /** * static logger class * * @version 0.1.0 * @static * @package * @subpackage factory * @see PEAR:Log */ class wbLog { /** * A convenience function for logging a debug event. * It will log a message at the PEAR_LOG_DEBUG log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function debug( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_DEBUG ); } /** * A convenience function for logging an info event. * It will log a message at the PEAR_LOG_INFO log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function info( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_INFO ); } /** * A convenience function for logging a notice event. * It will log a message at the PEAR_LOG_NOTICE log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function notice( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_NOTICE ); } /** * A convenience function for logging a warning event. * It will log a message at the PEAR_LOG_WARN log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function warning( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_WARNING ); } /** * A convenience function for logging a error event. * It will log a message at the PEAR_LOG_ERR log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function err( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_ERR ); } /** * A convenience function for logging a critical event. * It will log a message at the PEAR_LOG_CRIT log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function crit( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_CRIT ); } /** * A convenience function for logging a alert event. * It will log a message at the PEAR_LOG_ALERT log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function alert( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_ALERT ); } /** * A convenience function for logging a emergency event. * It will log a message at the PEAR_LOG_EMERG log level. * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @return bool $result true on success * @see log() */ function emerg( $loggerId, $msg ) { return wbLog::log( $loggerId, $msg, PEAR_LOG_EMERG ); } /** * universal log message function * * @static * @access public * @param string $loggerId named logger to be used * @param mixed $msg log message as array or string * @param int $priority log level * @return bool $result true on success */ function log( $loggerId, $msg, $priority = null ) { if( !wbFactory::getParam( 'withLog' ) ) { return true; } $log =& wbLog::getLogger( $loggerId ); if( !$log ) { return false; } if( is_array( $msg ) ) { $msg = implode( $GLOBALS['_wbLog']['delimiter'], $msg ); } if( $priority === null ) { $log->log( $msg ); } else { $log->log( $msg, $priority ); } return true; } /** * find running logger or start one * * implements a factory function to create used loggers on the fly * * @static * @access public * @param string $loggerId named logger to be used * @return bool $result true on success */ function &getLogger( $loggerId ) { if( !isset( $GLOBALS['_wbLog']['logger'][$loggerId] ) ) { // initial fake logger $GLOBALS['_wbLog']['logger'][$loggerId] = false; $logConf = $GLOBALS['_wbLog']['conf']; $logFile = wbFactory::getParam( 'baseDir' ) . '/' . wbFactory::getParam( 'varDir' ) . '/' . $GLOBALS['_wbLog']['logDir'] . '/'. $loggerId . '.wombat.log'; $logger =& Log::singleton( 'file', $logFile , $loggerId, $logConf ); if( !$logger ) { patErrorManager::raiseWarning( 'wbLog:1', 'Could not create logger', 'Failed to instantiate logger: "'. $loggerId .'"' ); } else { $GLOBALS['_wbLog']['logger'][$loggerId] = $logger; } } return $GLOBALS['_wbLog']['logger'][$loggerId]; } } ?>