Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.44 KB | None | 0 0
  1. <?php
  2.     define( "DEBUG", true );
  3.  
  4.     class Logger
  5.     {
  6.         private $_traceOutput;
  7.         private $_debugOutput;
  8.         private $_isDebugActive;
  9.  
  10.         static private $_instance;
  11.  
  12.         static public function init( $traceOutput = STDOUT , $debugOutput = STDERR )
  13.         {
  14.             self::$_instance = new Logger( $traceOutput, $debugOutput );
  15.         }
  16.  
  17.         static public function getInstance( )
  18.         {
  19.             return self::$_instance;
  20.         }
  21.  
  22.         static public function trace( $msg )
  23.         {
  24.             self::$_instance->traceMessage( $msg );
  25.         }
  26.  
  27.         static public function debug( $msg )
  28.         {
  29.             self::$_instance->debugMessage( $msg );
  30.         }
  31.  
  32.         public function __construct( $traceOutput, $debugOutput )
  33.         {
  34.             $this->_traceOutput = $traceOutput;
  35.             $this->_debugOutput = $debugOutput;
  36.  
  37.             // _isDebugActive indica se siamo in debug mode o meno
  38.             $this->_isDebugActive = DEBUG;
  39.         }
  40.  
  41.         public function __destruct()
  42.         {
  43.             fclose( $this->_debugOutput );
  44.             fclose( $this->_traceOutput );
  45.         }
  46.  
  47.         public function getDebugModeIsOn()
  48.         {
  49.             return $this->_isDebugActive;
  50.         }
  51.  
  52.         private function writeOnDebug( $handle, $message )
  53.         {
  54.             if( $this->getDebugModeIsOn() )
  55.             {
  56.                 fwrite( $handle, $message );
  57.             }
  58.         }
  59.  
  60.         public function traceMessage( $message )
  61.         {
  62.             $this->writeOnDebug( $this->_traceOutput, $message );
  63.         }
  64.  
  65.         public function debugMessage( $message )
  66.         {
  67.             $this->writeOnDebug( $this->_debugOutput, $message );
  68.         }
  69.     }
  70.  
  71.     Logger::init();
  72.     Logger::debug( "lol\n" );
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement