Advertisement
Guest User

Untitled

a guest
Nov 15th, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2. namespace MyApp\Core;
  3. use Fuel\Core as Fuel;
  4. use Auth as Auth;
  5.  
  6. class LogDatabase extends Fuel\Log
  7. {
  8.     /**
  9.      * Logs a message with the Info Log Level
  10.      *
  11.      * @param   string  $msg     The log message
  12.      * @param   string  $method  The method that logged
  13.      * @param   Exception  $e    The exception thrown
  14.      * @return  bool    If it was successfully logged
  15.      */
  16.     public static function info($msg, $method, $e = null)
  17.     {
  18.         return static::write(Fuel\Fuel::L_INFO, $msg, $method, $e);
  19.     }
  20.    
  21.     /**
  22.      * Logs a message with the Debug Log Level
  23.      *
  24.      * @param   string  $msg     The log message
  25.      * @param   string  $method  The method that logged
  26.      * @param   Exception  $e    The exception thrown
  27.      * @return  bool    If it was successfully logged
  28.      */
  29.     public static function debug($msg, $method, $e = null)
  30.     {
  31.         return static::write(Fuel\Fuel::L_DEBUG, $msg, $method, $e);
  32.     }
  33.    
  34.     /**
  35.      * Logs a message with the Warning Log Level
  36.      *
  37.      * @param   string  $msg     The log message
  38.      * @param   string  $method  The method that logged
  39.      * @param   Exception  $e    The exception thrown
  40.      * @return  bool    If it was successfully logged
  41.      */
  42.     public static function warning($msg, $method, $e = null)
  43.     {
  44.         return static::write(Fuel\Fuel::L_WARNING, $msg, $method, $e);
  45.     }
  46.    
  47.     /**
  48.      * Logs a message with the Error Log Level
  49.      *
  50.      * @param   string  $msg     The log message
  51.      * @param   string  $method  The method that logged
  52.      * @param   Exception  $e    The exception thrown
  53.      * @return  bool    If it was successfully logged
  54.      */
  55.     public static function error($msg, $method, $e = null)
  56.     {
  57.         return static::write(Fuel\Fuel::L_ERROR, $msg, $method, $e);
  58.     }
  59.  
  60.     /**
  61.      * Write Log File
  62.      *
  63.      * Generally this function will be called using the global log_message() function
  64.      *
  65.      * @access  public
  66.      * @param   int|string  the error level
  67.      * @param   string  the error message
  68.      * @param   string  information about the method
  69.      * @param   Exception  $e    The exception thrown
  70.      * @return  bool
  71.      */
  72.     public static function write($level, $msg, $method, $e = null)
  73.     {
  74.         // Use the parent write method to determine whether or not to log.
  75.         if (!parent::write($level, $msg, $method))
  76.         {
  77.             return false;
  78.         }
  79.        
  80.         // set level text.
  81.         $labels = array(
  82.                 1  => 'Error',
  83.                 2  => 'Warning',
  84.                 3  => 'Debug',
  85.                 4  => 'Info',
  86.         );
  87.         $level = $labels[$level];
  88.        
  89.         // set a method if it wasn't already set.
  90.         if(empty($method))
  91.         {
  92.             $method = \Input::uri();
  93.         }
  94.        
  95.         $log = Fuel\Log::forge();
  96.         $user_id = Auth\Auth::instance()->get_user_id();
  97.         $log->user_id = $user_id[1];
  98.         $log->occurred_on = \Date::forge()->get_timestamp();
  99.         $log->message = $msg;
  100.         $log->type = $level;
  101.         $log->activity = $method;
  102.         $log->stacktrace = ($e != null) ? $e->getTraceAsString() : 'N/A';
  103.         $log->ip_address = \Input::ip();
  104.         $log->save();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement