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

[eluser]ThatAirplaneGuy[/eluser]
I would like to attach some information from the current user session with every log_message that is created. Example: I have username stored in the session, I would like to attach that username to every error log entry that is created under that user's session. I tried creating the MY_Log in /libraries below:

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

class MY_Log extends CI_Log {
public function __construct()
{
  parent::__construct();
}


public function write_log($level = 'error', $msg, $php_error = FALSE)
{
  if ($this->_enabled === FALSE)
  {
   return FALSE;
  }

  $level = strtoupper($level);

  if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  {
   return FALSE;
  }

  $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
  $message  = '';

  if ( ! file_exists($filepath))
  {
   $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
  }

  if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
  {
   return FALSE;
  }

  $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --&gt; '.$this->session->userdata('USERNAME'). ' - '  .$msg."\n";

  flock($fp, LOCK_EX);
  fwrite($fp, $message);
  flock($fp, LOCK_UN);
  fclose($fp);

  @chmod($filepath, FILE_WRITE_MODE);
  return TRUE;
}

}

I get the error that it can't access $session. It looks like the session isn't available.

Is what I'm looking for possible with CI?

Thanks..

Edit: Here is the actual error message(s):

Code:
Notice: Undefined property: MY_Log::$session in E:\Web\myapp\application\libraries\MY_Log.php on line 37

Fatal error: Call to a member function userdata() on a non-object in E:\Web\myapp\application\libraries\MY_Log.php on line 37
#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:
&lt;?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);
}

}
#3

[eluser]ThatAirplaneGuy[/eluser]
Excellent!

Sorry for the delayed response - got pulled off onto another project for a couple of days.

Thanks again for the solution!

CJ




Theme © iAndrew 2016 - Forum software by © MyBB