Welcome Guest, Not a member yet? Register   Sign In
Session data available in MY_Log?
#2

[eluser]Aken[/eluser]
You're running into two problems here:

1) You can't use $this->session in your Log library, because it does not extend the CI superobject like a controller or model. You'll need to use get_instance() as explained in Utilizing CodeIgniter Resources within Your Library.

2) The Log library is used many times both before the CI superobject exists, and before any libraries are autoloaded to that CI object. So you need to check if both exist before trying to access them, or you'll get errors. Try something like this:

/application/libraries/MY_Log.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Log extends CI_Log {

public function write_log($level = 'error', $msg, $php_error = FALSE)
{
  // Check if CI singleton is available.
  if (class_exists('CI_Controller', false))
  {
   // Singleton is available. See if session library is loaded.
   $CI =& get_instance();
  
   if (isset($CI->session))
   {
    // Session library is available.
    // Prepend the username session data to the log message.
    $msg = $CI->session->userdata('USERNAME') . ' - ' . $msg;
   }
  }
  
  return parent::write_log($level, $msg, $php_error);
}

}


Messages In This Thread
Session data available in MY_Log? - by El Forum - 10-29-2012, 12:19 PM
Session data available in MY_Log? - by El Forum - 10-29-2012, 04:01 PM
Session data available in MY_Log? - by El Forum - 11-01-2012, 10:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB