/**
  * Get application Log (lazy-loaded)
  * @return Slim_Log
  */
 public function getLog()
 {
     if (!isset($this->log)) {
         $this->log = new Slim_Log();
         $this->log->setEnabled($this->config('log.enable'));
         $logger = $this->config('log.logger');
         if ($logger) {
             $this->log->setLogger($logger);
         } else {
             $this->log->setLogger(new Slim_Logger($this->config('log.path'), $this->config('log.level')));
         }
     }
     return $this->log;
 }
Example #2
0
 public function testLogFatal()
 {
     $this->expectOutputString('Fatal');
     $log = new Slim_Log(new MyWriter());
     $result = $log->fatal('Fatal');
     $this->assertTrue($result);
 }
Example #3
0
File: Slim.php Project: ntdt/Slim
 /**
  * Initialize Slim
  *
  * This instantiates the Slim application using the provided
  * application settings if available. This also:
  *
  * - Sets a default Not Found handler
  * - Sets a default Error handler
  * - Sets the view class
  *
  * Legacy Support:
  *
  * To support applications built with an older version of Slim,
  * this method's argument may also be a string (the name of a View class)
  * or an instance of a View class or subclass.
  *
  * @param   array|string|Slim_View  $viewClass   An array of settings;
  *                                               The name of a View class;
  *                                               A View class or subclass instance;
  * @return  void
  */
 public static function init( $userSettings = array() ) {
     //Legacy support
     if ( is_string($userSettings) || $userSettings instanceof Slim_View ) {
         $settings = array('view' => $userSettings);
     } else {
         $settings = (array)$userSettings;
     }
     self::$app = new Slim($settings);
     self::notFound(array('Slim', 'defaultNotFound'));
     self::error(array('Slim', 'defaultError'));
     self::view(Slim::config('view'));
     if ( Slim::config('log.enable') === true ) {
         $logger = Slim::config('log.logger');
         if ( empty($logger) ) {
             Slim_Log::setLogger(new Slim_Logger(Slim::config('log.path'), Slim::config('log.level')));
         } else {
             Slim_Log::setLogger($logger);
         }
     }
 }
Example #4
0
 /**
  * Test Slim initialization
  *
  * Pre-conditions:
  * Case A: Slim application initialized with logging, with custom Logger
  *
  * Post-conditions:
  * Case A: Custom Logger is set
  */
 public function testSlimInitWithCustomLogger() {
     Slim::init(array(
         'log.enable' => true,
         'log.logger' => new CustomLogger()
     ));
     $this->assertTrue(Slim_Log::getLogger() instanceof CustomLogger);
 }
 /**
  * Constructor
  * @param   array   $userSettings   Key-Value array of application settings
  * @return  void
  */
 public function __construct($userSettings = array())
 {
     //Setup Slim application
     $this->environment = Slim_Environment::getInstance();
     $this->request = new Slim_Http_Request($this->environment);
     $this->response = new Slim_Http_Response();
     $this->router = new Slim_Router($this->request->getResourceUri());
     $this->settings = array_merge(self::getDefaultSettings(), $userSettings);
     $this->middleware = array($this);
     $this->add(new Slim_Middleware_Flash());
     $this->add(new Slim_Middleware_MethodOverride());
     //Determine application mode
     $this->getMode();
     //Setup view
     $this->view($this->config('view'));
     //Make default if first instance
     if (is_null(self::getInstance())) {
         $this->setName('default');
     }
     //Set default logger that writes to stderr (may be overridden with middleware)
     $logWriter = $this->config('log.writer');
     if (!$logWriter) {
         $logWriter = new Slim_LogWriter($this->environment['slim.errors']);
     }
     $log = new Slim_Log($logWriter);
     $log->setEnabled($this->config('log.enabled'));
     $log->setLevel($this->config('log.level'));
     $this->environment['slim.log'] = $log;
     //Set global error handler
     set_error_handler(array('Slim', 'handleErrors'));
 }
Example #6
0
 public function testGetAndSetWriter()
 {
     $writer1 = new MyWriter();
     $writer2 = new MyWriter();
     $log = new Slim_Log($writer1);
     $this->assertSame($writer1, $log->getWriter());
     $log->setWriter($writer2);
     $this->assertSame($writer2, $log->getWriter());
 }
Example #7
0
 /**
  * Test Log adapter methods if no logger set
  *
  * Pre-conditions
  * Log instantiated without associated Logger
  *
  * Post-conditions:
  * All Log adapter methods return false
  */
 public function testLoggerMethodsIfNoLogger()
 {
     $log = new Slim_Log();
     $this->assertFalse($log->debug('Test'));
     $this->assertFalse($log->info('Test'));
     $this->assertFalse($log->warn('Test'));
     $this->assertFalse($log->error('Test'));
     $this->assertFalse($log->fatal('Test'));
 }
Example #8
0
 /**
  * Initialize Slim
  *
  * This instantiates the Slim application using the provided
  * application settings if available.
  *
  * Legacy Support:
  *
  * To support applications built with an older version of Slim,
  * this method's argument may also be a string (the name of a View class)
  * or an instance of a View class or subclass.
  *
  * @param   array|string|Slim_View  $viewClass   An array of settings;
  *                                               The name of a View class;
  *                                               A View class or subclass instance;
  * @return  void
  */
 public static function init($userSettings = array())
 {
     //Legacy support
     if (is_string($userSettings) || $userSettings instanceof Slim_View) {
         $settings = array('view' => $userSettings);
     } else {
         $settings = (array) $userSettings;
     }
     //Init app
     self::$app = new Slim($settings);
     //Init Not Found and Error handlers
     self::notFound(array('Slim', 'defaultNotFound'));
     self::error(array('Slim', 'defaultError'));
     //Init view
     self::view(Slim::config('view'));
     //Init logging
     if (Slim::config('log.enable') === true) {
         $logger = Slim::config('log.logger');
         if (empty($logger)) {
             Slim_Log::setLogger(new Slim_Logger(Slim::config('log.path'), Slim::config('log.level')));
         } else {
             Slim_Log::setLogger($logger);
         }
     }
     //Start session if not already started
     if (session_id() === '') {
         $sessionHandler = Slim::config('session.handler');
         if ($sessionHandler instanceof Slim_Session_Handler) {
             $sessionHandler->register();
         }
         session_start();
         if (isset($_COOKIE[session_id()])) {
             Slim::deleteCookie(session_id());
         }
         session_regenerate_id(true);
     }
     //Init flash messaging
     self::$app->flash = new Slim_Session_Flash(self::config('session.flash_key'));
     self::view()->setData('flash', self::$app->flash);
     //Determine mode
     if (isset($_ENV['SLIM_MODE'])) {
         self::$app->mode = (string) $_ENV['SLIM_MODE'];
     } else {
         $configMode = Slim::config('mode');
         self::$app->mode = $configMode ? (string) $configMode : 'development';
     }
 }
Example #9
0
 /**
  * Test Log adapter methods if no logger set
  *
  * Pre-conditions
  * Logger not set
  *
  * Post-conditions:
  * All calls to adapter return false
  */
 public function testLoggerMethodsIfNoLogger()
 {
     Slim_Log::setLogger(null);
     $this->assertFalse(Slim_Log::debug('Test'));
     $this->assertFalse(Slim_Log::info('Test'));
     $this->assertFalse(Slim_Log::warn('Test'));
     $this->assertFalse(Slim_Log::error('Test'));
     $this->assertFalse(Slim_Log::fatal('Test'));
 }
Example #10
0
 /**
  * Set the Logger object
  *
  * @param   mixed   $logger Instance of your custom Logger
  * @return  void
  */
 public static function setLogger($logger)
 {
     self::$logger = $logger;
 }